Lodash _.isEmpty() Method

The Lodash _.isEmpty() Method Checks if the value is an empty object, collection, map, or set. Objects are considered empty if they have no own enumerable string keyed properties. Collections are considered empty if they have a 0 length. Similarly, maps and sets are considered empty if they have a 0 size.

Syntax:

_.isEmpty( value )

Parameters: This method accepts a single parameter as mentioned above and described below:

  • value: This parameter holds the value that needs to be Checked for Empty value.

Return Value: This method returns a Boolean value(Returns true if the given value is an Empty value, else false).

Example 1: This method returns true when the value is null.



filter_none

edit
close

play_arrow

link
brightness_4
code

// Defining Lodash variable
const _ = require('lodash'); 
     
// Checking for Empty Value
console.log("The Value is Empty : "
        +_.isEmpty(null));

chevron_right


Output:

The Value is Empty : true

Example 2: This method returns true when an Array is empty.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Defining Lodash variable
const _ = require('lodash'); 
     
var val = []
  
// Checking for Empty Value
console.log("The Value is Empty : "
        +_.isEmpty(val));

chevron_right


Output:

The Value is Empty : true

Example 3: This method returns true for Boolean value.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Defining Lodash variable
const _ = require('lodash'); 
     
var val = false;
  
// Checking for Empty Value
console.log("The Value is Empty : " 
        +_.isEmpty(val));

chevron_right


Output:

The Value is Empty : true

Example 4: In this example, the method will return false.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Defining Lodash variable
const _ = require('lodash'); 
     
var val = { "a": 1 };
  
// Checking for Empty Value
console.log("The Value is Empty : " 
        +_.isEmpty(val)); 

chevron_right


Output:

The Value is Empty : false

Note: This will not work in normal JavaScript because it requires the lodash library to be installed. 

Lodash library can be installed using npm install lodash.

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.