Open In App

Underscore.js _.isEmpty() Function

Improve
Improve
Like Article
Like
Save
Share
Report

Underscore.js _.isEmpty() function is used to check whether a list, array, string, object, etc is empty or not. It first finds out the length of the passed argument and then decides. If the length is zero, then the output is true otherwise false.

Syntax: 

_.isEmpty(object);

Parameters:

It takes only one argument which is the object.

Return value:

It returns true if the argument passed is empty, i.e., does not have any elements in it. Otherwise it returns false.

Passing an empty element to the _.isEmpty() function:

The _.isEmpty() function takes the element from the list one by one and starts counting the length of the array. Each time it encounters an element, it increments length by one. Then, when the array finishes, it checks if the array’s length is zero ( it returns true) or greater than zero (then, it returns false). Here, we have an empty array so the output will be true.

Example: It shows the use of the Underscore.js _.isEmpty() Function by passing the empty element to it.

html




<!-- Write HTML code here -->
<html>
 
<head>
    <script src =
    </script>
</head>
 
<body>
    <script type="text/javascript">
        console.log(_.isEmpty([]));
    </script>
</body>
 
</html>


Output:

Passing an array with 6 elements to the _.isEmpty() function:

The procedure for the check function will be same as in the above example. Here, we have 6 elements in the array which implies that at the finish of the array, it’s length will be 6. so, the length is not equal to 0 and hence the answer will be false.

Example: It shows the use of the Underscore.js _.isEmpty() Function by passing an array with 6 elements.

html




<!-- Write HTML code here -->
<html>
 
<head>
    <script src =
    </script>
</head>
 
<body>
    <script type="text/javascript">
        console.log(_.isEmpty([1, 2, 3, 4, 5, 6]));
    </script>
</body>
 
</html>


Output:

Passing a list of characters to _.isEmpty() function:

The _.isEmpty() function will work the same as in the examples above. It implies that it does not distinguish between whether the array has numbers, characters or is empty. It will work the same on all the array and find out their length. In this example we have an array of length 4. Hence, the output will be false.

Example: It shows the use of the Underscore.js _.isEmpty() Function by passing a list of characters to it.

html




<html>
 
<head>
    <script src =
    </script>
</head>
 
<body>
    <script type="text/javascript">
        console.log(_.isEmpty(['HTML', 'CSS', 'JS', 'AJAX']));
    </script>
</body>
 
</html>


Output:

Passing an element zero to the _.isEmpty() function:

Do not get confused with the empty array and an array containing zero as an element. Since the elements is zero, so you must be thinking that the array is empty. But the array contains an element and since the _isEmpty() calculates the length, therefore the length of the below array will be one which is greater than zero. And hence the output will be false.

Example: It shows the use of the Underscore.js _.isEmpty() Function by passingan element zero to it.

html




<!-- Write HTML code here -->
 
<html>
 
<head>
    <script src =
    </script>
</head>
 
<body>
    <script type="text/javascript">
        console.log(_.isEmpty([0]));
    </script>
</body>
 
</html>


Output:

NOTE: 

These commands will not work in Google console or in firefox as for these additional files need to be added which they didn’t have added. So, add the given links to your HTML file and then run them. The links are as follows: 

<script type="text/javascript" src =
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</script>

An example is shown below:



Last Updated : 14 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads