Underscore.js _.isEmpty() Function
Last Updated :
14 Jan, 2024
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
< 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
< 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
< 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:

Similar Reads
Underscore.js _.isSet() Function
Underscore.js is a library in javascript that makes operations on arrays, string, objects much easier and handy. _.isSet() function is used to check whether the given object is javascript set or not. When linking the underscore.js CDN The "_" is attached to the browser as global variable. Syntax: _.
2 min read
Underscore.js _.isMap() Function
Underscore.js is a library in javascript that makes operations on arrays, string, objects much easier and handy. _.isMap() function is used to check whether the given object is javascript Map or not. Note: It is very necessary to link the underscore CDN before going and using underscore functions in
2 min read
Underscore.js _.isElement() Function
The _.isElement() function: is used to check whether the element is a document object model or not. A document object model is the way javascript sees the data of the containing pages. The Cascading style sheet (CSS) and javascript (JS) interact with Document object model (DOM). Syntax:_.isElement(o
3 min read
Underscore.js _.isWeakMap() Function
Underscore.js is a library in javascript that makes operations on arrays, string, objects much easier and handy. _.isWeakMap() function is used to check whether the given object is javascript weakmap or not. When linking the underscore.js CDN The "_" is attached to the browser as global variable. Sy
2 min read
Underscore.js _.isMatch() Function
It _.isMatch() function: is used to find out if the property given in argument is present in the passed array or not. Also, the property's value should be the same in order to match. It is used in cases where we want to find whether the array satisfies a specific condition or not. Syntax:_.isMatch(o
3 min read
Underscore.js _.isObject() Function
Underscore.js_.isObject() function is used to check whether the given object is an object or not. It returns a Boolean value True if the given object element is an object and returns False otherwise. JavaScript functions and arrays are objects, while the strings and numbers are not objects. Syntax:
1 min read
Underscore.js _.isRegExp() Function
_.isRegExp() function: It finds whether the object passed is a regular expression or not. If the object is a regular expression then it returns true otherwise false. We can even apply operations like addition etc on the variables in which the result of _.isRegExp() is stored. Syntax: _.isRegExp(obje
3 min read
Underscore.js _.isDate() function
Underscore.js is a javascript library that is capable enough to handle arrays, strings, objects, map, set very easily and efficiently. The _.isDate() function in underscore.js is used to tell if the given object is a date object or not. Syntax: _.isDate(object); Parameters: It takes only one paramet
1 min read
Underscore.js _.isError() Function
Underscore.js is a library in javascript that makes operations on arrays, string, objects much easier and handy. _.isError() function is used to check whether the given object is javascript Error Object or not. Note: It is very necessary to link the underscore CDN before going and using underscore f
2 min read
Underscore.js _.isArray() Function
The Underscore.js is a JavaScript library that provides a lot of useful functions like the map, filter, invoke, etc even without using any built-in objects. The _.isArray() function is used to find whether the passed argument is an array or not. An array is a set of variables, constants, and special
3 min read