Sub topic 5
NaN, Infinity, and -0
This chapter assumes you know basic JavaScript syntax, can run code in Node.js or a browser console, and understand === and typeof. It also assumes you have read the earlier chapters on how the Number type stores values as IEEE 754 doubles and on the arithmetic operators.
IEEE 754 defines three kinds of values that are not ordinary numbers: NaN, the two infinities, and negative zero. They are all of type "number", so they pass through your code without raising errors. You have to know where they come from and how to detect each one.
NaN: the result of invalid math
NaN means "Not a Number." It is what you get when an operation has no meaningful numeric result. NaN is contagious: any arithmetic that involves NaN also produces NaN.
console.log(typeof NaN); // "number"
console.log(0 / 0); // NaN
console.log(Math.sqrt(-1)); // NaN
console.log(Infinity - Infinity); // NaN
console.log(Number("abc")); // NaN
console.log(NaN + 1); // NaN
console.log(NaN === NaN); // false
console.log(Number.isNaN(NaN)); // true
console.log(isNaN("abc")); // true (coerces the string first)
console.log(Number.isNaN("abc")); // false (no coercion)
console.log([NaN].indexOf(NaN)); // -1
console.log([NaN].includes(NaN)); // trueNaN is the only JavaScript value that is not equal to itself, so x === NaN is always false. Use Number.isNaN(x) to test for it. The older global isNaN first converts its argument to a number, which means it reports true for any string that fails to parse. Array methods also disagree. indexOf uses === and never finds NaN, while includes uses SameValueZero equality and does find it.
Infinity: overflow and division by zero
Dividing a nonzero number by zero does not throw an error. It returns Infinity or -Infinity. A result that grows past Number.MAX_VALUE (about 1.8e308) overflows to Infinity in the same way.
console.log(1 / 0); // Infinity
console.log(-1 / 0); // -Infinity
console.log(Number.MAX_VALUE * 2); // Infinity
console.log(Infinity * 0); // NaN
console.log(Infinity > 1e308); // true
console.log(Number.isFinite(1 / 0)); // falseInfinity compares as larger than every finite number, so it works as a starting value when you search for a minimum. It is rarely what you want as a final answer, though. Check computed results with Number.isFinite, which returns false for Infinity, -Infinity, and NaN.
Negative zero
IEEE 754 stores a sign bit even for zero, so -0 is a separate value. You get it by multiplying or dividing zero by a negative number, or by rounding a small negative number toward zero.
console.log(0 * -1); // -0
console.log(Math.round(-0.4)); // -0
console.log(0 === -0); // true
console.log(Object.is(0, -0)); // false
console.log(1 / -0); // -Infinity
console.log(String(-0)); // "0"
console.log(JSON.stringify(-0)); // "0"=== treats 0 and -0 as equal, and converting -0 to a string drops its sign. It shows up again when you divide by it, or when you need the sign of a value that rounded to zero, such as a velocity or a temperature trend. Use Object.is when the sign matters. Object.is also reports Object.is(NaN, NaN) as true.
Summary
| Value | Common sources | Reliable check |
|---|---|---|
| NaN | `0/0`, failed parsing, `Math.sqrt(-1)` | `Number.isNaN(x)` |
| Infinity / -Infinity | `1/0`, overflow past `MAX_VALUE` | `!Number.isFinite(x) && !Number.isNaN(x)` |
| -0 | `0 * -1`, `Math.round(-0.4)` | `Object.is(x, -0)` |
Exercise
Write a function isNegativeZero(x) that returns true only for -0. Then explain why x === -0 cannot be used.
Show answer
function isNegativeZero(x) { return Object.is(x, -0); } Another version that also works is x === 0 && 1 / x === -Infinity. You cannot use x === -0 because === treats 0 and -0 as equal, so it would also return true for positive zero.
Exercise
What does [1, NaN, 3].indexOf(NaN) return, and what should you use instead to check whether the array contains NaN?
Show answer
It returns -1 because indexOf compares with ===, and NaN is never equal to itself. Use [1, NaN, 3].includes(NaN), which returns true, or arr.some(Number.isNaN).