← Back to JavaScript, page 7

javascript-0126 · Easy fresher

How do NaN, -0, and Object.is behave in JavaScript, and why does NaN not equal itself?

NaN means "not a number" and by the IEEE 754 standard it is not equal to anything, including itself, so use Number.isNaN or Object.is to test for it. JavaScript also has a signed zero: -0 equals 0 with ===, but Object.is tells them apart, and 1 divided by -0 is -Infinity. Object.is uses SameValue, while Map, Set, and includes use SameValueZero, which treats NaN as equal to NaN and -0 as equal to 0.

Full answer

NaN is the result of an invalid numeric operation, such as 0 / 0, Math.sqrt(-1), or Number('abc'). Under the IEEE 754 floating point standard, a NaN is unordered, so every comparison with it is false, including NaN === NaN. Its type is still 'number'.

Testing for NaN: the global isNaN(value) first converts the value to a number, so isNaN('abc') is true, which is misleading. Number.isNaN(value) returns true only for the actual NaN value and does no conversion. Object.is(value, NaN) also works. Prefer Number.isNaN.

Negative zero: floating point numbers have a sign bit, so -0 exists. It appears from -0 literals, 0 * -1, and Math.round(-0.4). The strict equality operator treats 0 === -0 as true, and String(-0) is '0', so it is easy to miss. It matters in 1 / -0, which is -Infinity, in Math.sign, Math.atan2, and in rounding of small negative values. Object.is(0, -0) is false.

Object.is(a, b) uses the SameValue algorithm: like ===, except that NaN equals NaN and +0 differs from -0. React uses it to compare state values.

There is a third rule, SameValueZero, used by Map keys, Set values, and Array.prototype.includes: NaN equals NaN, and +0 equals -0. But indexOf and findIndex with a value use ===, so [NaN].indexOf(NaN) is -1, while [NaN].includes(NaN) is true.

Other number edge cases that interviews cover: typeof NaN, Number('') is 0, Number(null) is 0, Number(undefined) is NaN, parseInt('12px') is 12 while Number('12px') is NaN, Math.min() is Infinity, Math.max() is -Infinity, and Number.isInteger(5.0) is true.

javascript
console.log(NaN === NaN, NaN == NaN, typeof NaN, Number.isNaN(NaN), Object.is(NaN, NaN));
console.log(isNaN('abc'), Number.isNaN('abc'), isNaN(''), Number.isNaN(''));
console.log(0 / 0, Math.sqrt(-1), Number('abc'), parseInt('12px'), Number('12px'));

console.log(0 === -0, Object.is(0, -0), Object.is(-0, -0), String(-0), `${-0}`);
console.log(1 / 0, 1 / -0, Math.sign(-0), Math.round(-0.4), Object.is(Math.round(-0.4), -0));
console.log(JSON.stringify(-0), JSON.stringify([-0]), [-0], -0 + 0, Object.is(-0 + 0, 0));

console.log([NaN].indexOf(NaN), [NaN].includes(NaN), [0].includes(-0), [-0].indexOf(0));
const set = new Set([NaN, NaN, 0, -0]);
console.log(set.size, [...set]);
const map = new Map([[NaN, 'nan key']]);
console.log(map.get(NaN), new Map([[-0, 'zero']]).get(0));

console.log(Number(''), Number(' '), Number(null), Number(undefined), Number(true), Number([]), Number([5]), Number({}));
console.log(Math.min(), Math.max(), Number.isInteger(5.0), Number.isInteger('5'), Number.isSafeInteger(2 ** 53));

// a safe equality helper: treat NaN as equal, keep 0 and -0 apart
const sameValue = (a, b) => Object.is(a, b);
console.log([1, NaN, -0].map((value) => sameValue(value, NaN)), [1, NaN, -0].map((value) => sameValue(value, 0)));

Run with Node.js v24.22.1 as js-126.js; two runs gave identical output. Output: false false number true true true false false false NaN NaN NaN 12 NaN true false true 0 0 Infinity -Infinity -0 -0 true 0 [0] [ -0 ] 0 true -1 true true 0 2 [ NaN, 0 ] nan key zero 0 0 0 NaN 1 0 5 NaN Infinity -Infinity true false false [ false, true, false ] [ false, false, false ]

Key points

  • NaN is not equal to anything, including itself; its typeof is number.
  • Use Number.isNaN, not the global isNaN, which converts first.
  • -0 exists; === treats it as equal to 0, Object.is does not, and 1 divided by -0 is -Infinity.
  • Object.is is SameValue; Map, Set, and includes use SameValueZero; indexOf uses strict equality.
  • Know the conversions: Number of empty string and null is 0, of undefined is NaN.

Common mistakes

  • Testing for NaN with x === NaN, which is always false.
  • Using the global isNaN on strings and getting true for any non-numeric text.
  • Using indexOf to look for NaN.
  • Forgetting that JSON and string conversion hide the sign of zero.

Follow-ups

  • What is the difference between SameValue and SameValueZero, and where is each used?
  • Why does Math.round of a small negative number give -0?
  • How would you detect -0 without Object.is?

Tips

  • Give the one-line reason: NaN is unordered by the floating point standard.
  • Recommend Number.isNaN and explain what the global isNaN does wrong.
  • Name the three equality algorithms and where each appears.