The objIs() method determines whether two values are the same value.

Two values are the same if one of the following holds:

  • both undefined
  • both null
  • both true or both false
  • both strings of the same length with the same characters in the same order
  • both the same object (meaning both values reference the same object in memory)
  • both numbers and both +0, both -0, both NaN, or both non-zero and both not NaN and both have the same value

This is different from the === operator in that:

  • NaN is equal to NaN
  • +0 is not equal to -0

0.11.9

// Case 1: NaN
objIs(NaN, NaN); // true
NaN === NaN; // false

// Case 2: Signed zeros
objIs(0, -0); // false
objIs(+0, -0); // false
objIs(-0, -0); // true
0 === -0; // true

// Regular comparison
objIs('hello', 'hello'); // true
objIs('hello', 'goodbye'); // false
objIs(1, 1); // true
objIs(1, 2); // false

// Objects
const obj = { a: 1 };
objIs(obj, obj); // true
objIs(obj, { a: 1 }); // false (different objects with same content)
  • Parameters

    • value1: any

      The first value to compare

    • value2: any

      The second value to compare

    Returns boolean

    True if the values are the same value, false otherwise