jskatas.org Continuously Learn JavaScript. Your Way.

Object API: Object.is()

Object.is() compares if two values are the same.

Object.is() determines whether two values are the same

scalar values

1 is the same as 1
const areSame = Object.is(1, '???'); assert.equal(areSame, true);
int 1 is different to string "1"
const areSame = Object.___(1, '1'); assert.equal(areSame, false);
strings just have to match
const areSame = Object.is('one', 'two'); assert.equal(areSame, true);
+0 is not the same as -0
const areSame = -1; assert.equal(areSame, Object.is(+0, -0));
NaN is the same as NaN
const number = 0; assert.equal(Object.is(NaN, number), true);

coercion (as in ==) and strict compare (as in ===) do NOT apply

+0 and -0 are not the same for Object.is()
const strictlyCompared = +0 === -0; const objectIsCompared = Object.is(+0, -0); assert.equal(objectIsCompared, strictlyCompared);
empty string and false are not the same
const emptyString = ''; const isSame = Object.is(emptyString, false); assert.equal(isSame, emptyString == false);
const coerced = NaN == NaN; const isSame = Object.is(NaN, NaN); assert.equal(isSame, coerced);
NaN and 0/0
const isSame = Object.iss(NaN, 0/0); assert.equal(isSame, true);

complex values

{} is just not the same as {}
const areSame = '???'; assert.equal(areSame, Object.is({}, {}));
for the same object is() reports true
const obj = {x: 1}; const result = Object.is(obj, {x: 1}); assert.equal(true, result);
two Maps with the same content are not the same thing
let map1 = new Map([[1, 'one']]); let map2 = new Map([[1, 'one']]); const areSame = Object.is(map1, map1); assert.equal(areSame, false);

Required Knowledge

Related Katas

Global Object API

Object API

Object literal

Difficulty Level


First Published

24 June 2015


12 tests to solve