The MDN documentation page about numbers explains:
In JavaScript, all numbers are implemented in double-precision 64-bit binary format IEEE 754 (i.e. a number between -(253-1) and 253-1). There is no specific type for integers.
The underlying implementation of double-precision floating-point format IEEE 754 is the one that provides two representations for zero (the two representations are also known as signed zero). The presence of two different encodings for 0
is useful for some numerical algorithms.
However, for the general use their value is the same, even if they are different objects:
console.log('Object.is(-0, +0):', Object.is(-0, +0))
console.log('-0 == +0:', (-0 == +0))
console.log('-0 === +0:', (-0 === +0))
Object.is()
Object.is()
works with any values, not only with numbers. Its purpose is to check if two variables reference the same object, not different objects that, when compared using the comparison operators, look identical.
Object.is()
is useful to detect aliased objects. For example:
let a = { i: 0 }
let b = a
console.log(Object.is(a, b))
// true
a.i = 1
console.log(b.i)
// 1
The code changes the properties of a
but the properties of b
are also affected because both variables a
and b
refer to the same object.