What is the _.isEmpty() method in Lodash?
Overview
The _.isEmpty() method in Lodash checks if a value is empty.
Syntax
_.isEmpty(value)
Parameters
This method accepts the following parameters:
value: This is the value to be checked.
Return value
This method returns true if the value is empty, and vice-versa.
If the value is a boolean or null, it will always return true.
If the value is a collection, this method will evaluate its length to determine if it’s empty.
Example
Let’s look at an example of the _.isEmpty() method in the code snippet below:
Explanation
In the HTML tab:
- Line 5: We import the
lodashscript.
In the JavaScript tab:
- Line 2: We create a variable
value1and initialize it withnull. - Line 5: We create a variable
value2and initialize it withtrue. - Line 8: We create an empty
array
value3. - Line 11: We create an
array
value4and initialize it with a few values. - Lines 14-17: We use the
_.isEmpty()method to check if the values are empty. Also, we print the output on the console.
Output
- The variable
value1isnull. Therefore,_.isEmpty()returnstrue. - The variable
value2is a boolean. Therefore,_.isEmpty()returnstrue. - The variable
value3is an empty array. Therefore,_.isEmpty()returnstrue. - The variable
value4is a non-empty array. Therefore,_.isEmpty()returnsfalse.