The “util” module provides ‘utility’ functions that are used for debugging purposes. For accessing those functions we need to call them (by ‘require(‘util’)‘).
The util.isDeepStrictEqual() (Added in v9.0.0) method is an inbuilt application programming interface of the util module which is an exported type function that tests the deep equality of two values that is, between the actual (value1) and expected (value2) parameters. Deep equality is a method that helps to evaluate the enumerable “own” properties of child objects recursively by a few rules.
Syntax:
const util = require('util');
util.isDeepStrictEqual(val1, val2);
Parameters: This function accepts two parameters as mentioned above and described below:
-
val1 <any>: Any variable, Class, Function, Object, or JavaScript primitive.
-
val2 <any>: Any variable, Class, Function, Object, or JavaScript primitive.
Return Value <boolean>: If value1 and value2 are deemed equal then returns true, otherwise returns false.
Example 1: Filename: index.js
const util = require( 'util' );
const object1 = {
alfa: "beta" ,
romeo: [10, 20]
};
const object2 = {
alfa: "beta" ,
romeo: [10, 20]
};
console.log( "1.>" , object1 == object2)
console.log( "2.>" , util
.isDeepStrictEqual(object1, object2))
const wrongDateType = {};
console.log( "3.>" , util.isDeepStrictEqual(
wrongDateType, Date.prototype));
const anObject = {};
console.log( "4.>" , util.isDeepStrictEqual(
anObject, wrongDateType));
const newDate = new Date();
console.log( "5.>" , util.isDeepStrictEqual(
newDate, wrongDateType));
const weakMapOne = new WeakMap();
const weakMapTwo = new WeakMap([[{}, {}]]);
console.log( "6.>" , util.isDeepStrictEqual(
weakMapOne, weakMapTwo));
|
Run index.js file using the following command:
node index.js
Output:
1.> false
2.> true
3.> true
4.> true
5.> false
6.> true
Example 2: Filename: index.js
const util = require( 'util' );
console.log( "1.>" , util
.isDeepStrictEqual({ a: 1 }, { a: '1' }));
console.log( "2.>" , util.isDeepStrictEqual(NaN, NaN));
console.log( "3.>" , util
.isDeepStrictEqual(Object(1), Object(2)));
const { isDeepStrictEqual } = require( 'util' );
console.log( "4.>" , isDeepStrictEqual(
Object( 'alfa' ), Object( 'alfa' )));
console.log( "5.>" , isDeepStrictEqual(-0, -0));
console.log( "6.>" , isDeepStrictEqual(0, -0));
|
Run index.js file using the following command:
node index.js
Output:
1.> false
2.> true
3.> false
4.> true
5.> true
6.> false
Reference: https://nodejs.org/api/util.html#util_util_isdeepstrictequal_val1_val2
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
28 Jul, 2020
Like Article
Save Article