Object.is Separates -0 From 0 And NaN
Object.is treats NaN as identical to itself and keeps -0 distinct from 0, the two places where the === comparison disagrees with intended identity.
Code
// === treats -0 and 0 as the same value; Object.is can tell them apart
console.log('-0 === 0:', -0 === 0);
console.log('Object.is(-0, 0):', Object.is(-0, 0));
// NaN is === false against everything, including itself; Object.is treats NaN as equal to itself
console.log('NaN === NaN:', NaN === NaN);
console.log('Object.is(NaN, NaN):', Object.is(NaN, NaN));
// 1 / -0 evaluates to -Infinity, one way to detect -0
const negative = -0;
console.log('1 / -0:', 1 / negative);
// Math.sign(-0) returns -0: === cannot tell them apart, Object.is can
console.log('sign(-0) is -0:', Object.is(Math.sign(-0), -0));
console.log('sign(-0) === 0:', Math.sign(-0) === 0);
// on ordinary values Object.is behaves exactly like ===
console.log('same:', Object.is('a', 'a'), Object.is(1, '1'));Output
Captured from a sandboxed run of this snippet in the build pipeline.
-0 === 0: true
Object.is(-0, 0): false
NaN === NaN: false
Object.is(NaN, NaN): true
1 / -0: -Infinity
sign(-0) is -0: true
sign(-0) === 0: true
same: true falseHow it works
The file’s state is a handful of top-level bindings: negative.
Using it
The run emits 8 labeled sections — -0 === 0:, Object.is(-0, 0):, NaN === NaN:, Object.is(NaN, NaN):, 1 / -0:, sign(-0) is -0:, sign(-0) === 0:, same: — one per step.
Key statements
const negative = -0;
Interactive Tools for This Topic
Number Base ConverterConvert arbitrary-size integers between binary, octal, decimal, hexadecimal, and base 36 with digit validation and signed input support.Number to WordsConvert an integer up to 999,999,999,999 into US English words, using the short scale and a minus prefix for negative values.Roman Numeral ConverterConvert integers between 1 and 3999 to Roman numerals and back, rejecting malformed numeral forms.Scientific Notation ConverterConvert numbers between plain decimal and scientific notation, optionally fixing the significant digits and splitting out the mantissa and exponent.
FAQ
- Are there dependencies to manage?
- No. There are no dependencies to declare or install. Its state lives in module-level bindings: `negative`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.