Node.js – util.isDeepStrictEqual() Method

Node.jsServer Side ProgrammingProgramming

The util.isDeepStrictEqual() method as the name suggests, is used for checking whether the two values are deep strict equal or not. If both the values are deep strict equal, true is returned else it returns false.

Syntax

util.isDeepStrictEqual(val1, val2)

Parameters

  • val1 & val2 – Both the input parameters can accept class, function, object or a JavaScript primitive value. The above function checks the equality between these two values.

Example 1

Create a file with the name "deepStrictEqual.js" and copy the following code snippet. After creating the file, use the command "node deepStrictEqual.js" to run this code.

// util.isDeepStrictEqual() Demo Example

// Importing the util module
const util = require('util');

// Creating an object
const val1 = {
   name: "tom",
   age: 21
};

// Creating another object
const val2 = {
   name: "tom",
   age: 21
};

// Checking if both are same objects
console.log("1.", val1 == val2)

// Checking equality within objects
console.log("2.", util.isDeepStrictEqual(val1, val2))

// Initializing empty data
const emptyData = {};

// Comparing empty data with date
console.log("3.", util.isDeepStrictEqual(
emptyData, Date.prototype));

// Getting today's date
const newDate = new Date();

// Comparing date and its prototype
console.log("4.", util.isDeepStrictEqual(
newDate, Date.prototype));

Output

C:\home\node>> node deepStrictEqual.js
1. false
2. true
3. true
4. false

Example 2

// util.isDeepStrictEqual() Demo Example

// Importing the util module
const util = require('util');

// Comparing string with integer
console.log("1. ", util
.isDeepStrictEqual({ a: 1 }, { a: '1' }));

// Comparing two NaN values
console.log("2. ", util.isDeepStrictEqual(NaN, NaN));

// Comparing two undefined values
console.log("3. ", util
.isDeepStrictEqual(undefined, undefined));

// Importing isDeepStrictEqual instead of whole util
const { isDeepStrictEqual } = require('util');

// Comparing two same unwrapped objects
console.log("4. ", isDeepStrictEqual(
Object('jerry'), Object('jerry')));

Output

C:\home\node>> node deepStrictEqual.js
1. false
2. true
3. true
4. true
raja
Published on 17-Aug-2021 11:17:14
Advertisements