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.
// Defining Lodash variable const _ = require( 'lodash' ); // Checking for Empty Value console.log( "The Value is Empty : " +_.isEmpty( null )); |
Output:
The Value is Empty : true
Example 2: This method returns true when an Array is empty.
// Defining Lodash variable const _ = require( 'lodash' ); var val = [] // Checking for Empty Value console.log( "The Value is Empty : " +_.isEmpty(val)); |
Output:
The Value is Empty : true
Example 3: This method returns true for Boolean value.
// Defining Lodash variable const _ = require( 'lodash' ); var val = false ; // Checking for Empty Value console.log( "The Value is Empty : " +_.isEmpty(val)); |
Output:
The Value is Empty : true
Example 4: In this example, the method will return false.
// Defining Lodash variable const _ = require( 'lodash' ); var val = { "a" : 1 }; // Checking for Empty Value console.log( "The Value is Empty : " +_.isEmpty(val)); |
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.
Please Login to comment...