The _.isEmpty()
method in Lodash checks if a value is empty.
_.isEmpty(value)
This method accepts the following parameters:
value
: This is the value to be checked.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.
Let’s look at an example of the _.isEmpty()
method in the code snippet below:
In the HTML tab:
lodash
script.In the JavaScript tab:
value1
and initialize it with null
.value2
and initialize it with true
.value3
.value4
and initialize it with a few values._.isEmpty()
method to check if the values are empty. Also, we print the output on the console.value1
is null
. Therefore, _.isEmpty()
returns true
.value2
is a boolean. Therefore, _.isEmpty()
returns true
.value3
is an empty array. Therefore, _.isEmpty()
returns true
.value4
is a non-empty array. Therefore, _.isEmpty()
returns false
.