Lodash _.isEqual() Method
Lodash _.isEqual() Method performs a deep comparison between two values to determine if they are equivalent. This method supports comparing arrays, array buffers, boolean, date objects, maps, numbers, objects, regex, sets, strings, symbols, and typed arrays.
Syntax:
_.isEqual( value1, value2);
Parameters:
- value1: First comparable value
- value2: Second comparable value
Return Value:
This method returns a Boolean value(Returns true if the two values are equal, else false).
Note: Objects are compared directly, their inherited or enumerable properties are not compared. Functions and DOM nodes are compared strictly by the ‘===’ operator.
Example 1: In this example, We are comparing two variables having objects as a value and printing results in the console.
Javascript
// Defining Lodash variable const _ = require( 'lodash' ); // First value let val1 = { "a" : "gfg" }; // Second value let val2 = { "b" : "gfg" }; // Checking for Equal Value console.log( "The Values are Equal : " + _.isEqual(val1, val2)); |
Output:
The Values are Equal : false
Example 2: In this example, We are comparing two variables having Array as a value and printing results in the console.
Javascript
// Defining Lodash variable const _ = require( 'lodash' ); let val1 = [1, 2, 3, 4] let val2 = [1, 2, 3, 4] // Checking for Equal Value console.log( "The Values are Equal : " + _.isEqual(val1, val2)); |
Output:
The Values are Equal : true
Example 3: In this example, We are comparing two variables having String as a value and printing results in the console.
Javascript
// Defining Lodash variable const _ = require( 'lodash' ); let val1 = "gfg" let val2 = "gfg" // Checking for Equal Value console.log( "The Values are Equal : " +_.isEqual(val1,val2)); |
Output:
The Values are Equal : true
Example 4: In this example, We are comparing two variables having Numbers as a value and printing results in the console.
Javascript
// Defining Lodash variable const _ = require( 'lodash' ); let val1 = 1 let val2 = 1 // Checking for Equal Value console.log( "The Values are Equal : " + _.isEqual(val1, val2)); |
Output:
The Values are Equal : true