Open In App

Node.js util.isDeepStrictEqual() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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




      
// Node.js syntax to demonstrate the 
// util.isDeepStrictEqual() method
  
// Importing util library
const util = require('util');
  
// Creating object1
const object1 = {
    alfa: "beta",
    romeo: [10, 20]
};
  
// Creating object2
const object2 = {
    alfa: "beta",
    romeo: [10, 20]
};
  
// Returns false
console.log("1.>", object1 == object2)
  
// Returns true
console.log("2.>", util
    .isDeepStrictEqual(object1, object2))
  
// Creating a fake date
const wrongDateType = {};
  
// Comparing wrongDateType with correct date
console.log("3.>", util.isDeepStrictEqual(
            wrongDateType, Date.prototype));
  
const anObject = {};
  
// Prototype is not same
console.log("4.>", util.isDeepStrictEqual(
            anObject, wrongDateType));
// Returns false
  
// Creating new date
const newDate = new Date();
  
// Comparing Date formats
console.log("5.>", util.isDeepStrictEqual(
            newDate, wrongDateType));
// Returns false
  
const weakMapOne = new WeakMap();
const weakMapTwo = new WeakMap([[{}, {}]]);
  
// Comparing two weakMaps  
console.log("6.>", util.isDeepStrictEqual(
            weakMapOne, weakMapTwo));
// Returns true


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




// Node.js syntax to demonstrate the
// util.isDeepStrictEqual() method
  
// Importing util library
const util = require('util');
  
// String and integer are compared
// wrong 1 !== '1'.
console.log("1.>", util
    .isDeepStrictEqual({ a: 1 }, { a: '1' }));
// Returns false 
  
// Comparing Not a Number with Not a Number
console.log("2.>", util.isDeepStrictEqual(NaN, NaN));
// Returns true
  
// Unwrapped numbers are different
console.log("3.>", util
    .isDeepStrictEqual(Object(1), Object(2)));
// Returns false
  
// Directly importing isDeepStrictEqual method
const { isDeepStrictEqual } = require('util');
  
// Unwrapped strings are same
console.log("4.>", isDeepStrictEqual(
        Object('alfa'), Object('alfa')));
// Returns true
  
// Comparing both negative values  
console.log("5.>", isDeepStrictEqual(-0, -0));
// Returns true
  
// Same Value Comparison with different sign
console.log("6.>", isDeepStrictEqual(0, -0));
// Returns false


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



Last Updated : 28 Jul, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads