Skip to content
Related Articles

Related Articles

How to check an array is empty or not using jQuery ?

Improve Article
Save Article
Like Article
  • Last Updated : 13 Jan, 2022

In this article, we will check if an array is empty or not using jQuery. In JavaScript, arrays are a special type of object. If we use the typeof operator for arrays, it returns “object”. We can use jQuery’s isEmptyObject() method to check whether the array is empty or contains elements.

The isEmptyObject() method accepts a single parameter of type Object, which is the object to be checked and returns a boolean value true if the object is empty and false if not empty.

Syntax: 

$.isEmptyObject(array);

Example 1: In the example below, we passed an empty array to the isEmptyObject() method.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" 
          content="IE=edge">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0">
     
    <!-- jQuery CDN Link -->
    <script src=
    </script>
  
    <style>
        body {
            margin: 30px;
            font-family: sans-serif;
            text-align: center;
        }
        button {
            padding: 20px;
            background-color: green;
            color: white;
            cursor: pointer;
        }
    </style>
  
    <!-- Function to check if array is empty -->
    <script>
        function checkArray() {
            var array = [];
  
            if($.isEmptyObject(array)) {
                $("#write").text("The Array is Empty.");
            }else {
                $("#write").text("The Array is not Empty.");
            }
        }
    </script>
</head>
<body>
    <button id="button1" onclick="checkArray();">
        CHECK ARRAY
     </button>
  
    <h2 id="write"></h2>
</body>
</html>

Output:

Example 2: In this example, we passed a non-empty array to the isEmptyObject() method.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" 
          content="IE=edge">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
    <title>
        Check whether the array is empty 
        or not using jQuery
    </title>
    <!-- jQuery CDN Link -->
    <script src=
    </script>
  
    <style>
        body {
            margin-top: 30px;
            font-family: sans-serif;
            text-align: center;
        }
  
        button {
            padding: 20px;
            background-color: green;
            color: white;
            cursor: pointer;
        }
    </style>
  
    <!-- Function to check if array is empty -->
    <script>
        function checkArray() {
            var array = [20, 49, "gfg"];
            if ($.isEmptyObject(array)) {
                $("#write").text("The Array is Empty.");
            } else {
                $("#write").text("The Array is not Empty.");
            }
        }
    </script>
</head>
  
<body>
    <button id="button1" onclick="checkArray();">
      CHECK ARRAY
    </button>
    <h2 id="write"></h2>
</body>
  
</html>

Output:


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!