revision:
The Object.is() static method determines whether two values are the same value.
Object.is(value1, value2)
Parameters:
value1 : required. The first value to compare.
value2 : required. The second value to compare..
console.log(Object.is('1', 1)); // Expected output: false console.log(Object.is(NaN, NaN)); // Expected output: true console.log(Object.is(-0, 0)); // Expected output: false const obj = {}; console.log(Object.is(obj, {})); // Expected output: false
<div> <p id="is-1"></p> <p id="is-2"></p> <p id="is-3"></p> <p id="is-4"></p> <p id="is-5"></p> <p id="is-6"></p> <p id="is-7"></p> <p id="is-8"></p> <p id="is-9"></p> <p id="is-10"></p> <p id="is-11"></p> <p id="is-12"></p> <p id="is-13"></p> <p id="is-14"></p> <p id="is-15"></p> </div> <script> // Case 1: Evaluation result is the same as using === Object.is(25, 25); // true document.getElementById('is-1').innerHTML = "same value (25, 25)? : " + Object.is(25, 25); Object.is("foo", "foo"); // true document.getElementById('is-2').innerHTML = "same value ('foo', 'foo')? : " + Object.is('foo', 'foo'); Object.is("foo", "bar"); // false document.getElementById('is-3').innerHTML = "same value ('foo', 'bar')? : " + Object.is('foo' , 'bar'); Object.is(null, null); // true document.getElementById('is-4').innerHTML = "same value (null, null)? : " + Object.is(null, null); Object.is(undefined, undefined); // true document.getElementById('is-5').innerHTML = "same value (undefined, undefined)? : " + Object.is(undefined, undefined); Object.is(window, window); // true document.getElementById('is-6').innerHTML = "same value (window, window)? : " + Object.is(window, window); Object.is([], []); // false document.getElementById('is-7').innerHTML = "same value ([], [])? : " + Object.is([], []); const foo = { a: 1 }; const bar = { a: 1 }; const sameFoo = foo; Object.is(foo, foo); // true document.getElementById('is-8').innerHTML = "same value (foo, foo)? : " + Object.is(foo, foo); Object.is(foo, bar); // false document.getElementById('is-9').innerHTML = "same value (foo, bar)? : " + Object.is(foo, bar); Object.is(foo, sameFoo); // true document.getElementById('is-10').innerHTML = "same value (foo, sameFoo)? : " + Object.is(foo, sameFoo); // Case 2: Signed zero Object.is(0, -0); // false document.getElementById('is-11').innerHTML = "same value (0, -0)? : " + Object.is(0, -0); Object.is(+0, -0); // false document.getElementById('is-12').innerHTML = "same value (+0, -0)? : " + Object.is(+0, -0); Object.is(-0, -0); // true document.getElementById('is-13').innerHTML = "same value (-0, -0)? : " + Object.is(-0, -0); // Case 3: NaN Object.is(NaN, 0 / 0); // true document.getElementById('is-14').innerHTML = "same value (NaN, 0/0)? : " + Object.is(NaN, 0/0); Object.is(NaN, Number.NaN); // true document.getElementById('is-15').innerHTML = "same value (NaN, Number.NaN)? : " + Object.is(NaN, Number.NaN); </script>