Object.isLoading saved progress…

Object.is

Object.is reports whether two values are the same value. It is almost identical to === (strict equality), with exactly two deliberate differences: it calls NaN equal to NaN, and it calls +0 and -0 different. Those two cases are the only places where strict equality gives a result that disagrees with same-value equality, and handling them is the whole point of the method.

Implement objectIs(a, b). Return true when a and b are the same value and false otherwise, matching === in every case except the two above. Build it yourself — don't call the built-in Object.is.

Signature

function objectIs(a, b) {
  // returns true if a and b are the "same value", else false
}

Examples

objectIs(1, 1);        // true
objectIs('a', 'a');    // true
objectIs(1, '1');      // false  — different types, just like ===
objectIs(NaN, NaN);    // true   — === would say false
objectIs(0, -0);       // false  — === would say true
objectIs(-0, -0);      // true
objectIs({}, {});      // false  — two different objects
const o = {};
objectIs(o, o);        // true   — the same reference

Notes

  • Matches === almost everywhere — the same primitive value is true, different types or values are false, two different objects are false, and the same reference is true.
  • NaN equals NaN — the first difference from ===, which reports NaN === NaN as false.
  • +0 and -0 differ — the second difference: objectIs(+0, -0) is false, even though +0 === -0 is true.
  • Not == — this is same-value equality, never loose equality. No type coercion happens, so objectIs(0, false) and objectIs(null, undefined) are both false.
  • null and undefined — each is the same value as itself, but not as the other.
Loading editor…