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:

Console

Explanation

In the HTML tab:

  • Line 5: We import the lodash script.

In the JavaScript tab:

  • Line 2: We create a variable value1 and initialize it with null.
  • Line 5: We create a variable value2 and initialize it with true.
  • Line 8: We create an empty array value3.
  • Line 11: We create an array value4 and 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 value1 is null. Therefore, _.isEmpty() returns true.
  • The variable value2 is a boolean. Therefore, _.isEmpty() returns true.
  • The variable value3 is an empty array. Therefore, _.isEmpty() returns true.
  • The variable value4 is a non-empty array. Therefore, _.isEmpty() returns false.

Free Resources