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.

JavascriptData Integrity

Code

javascriptsnippet
18 lines
// === 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 false

How 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

FAQ

Are there dependencies to manage?
No. There are no dependencies to declare or install. Its state lives in module-level bindings: `negative`.