Sub topic 10
Edge Cases: NaN, -0, null, and undefined
This chapter assumes you already know how ===, ==, and Object.is work. It covers the four values where those operators give unexpected answers. Most equality bugs in real code involve one of these four values.
NaN: the value that is not equal to itself
NaN means "Not a Number." You get it when a numeric operation fails, such as Number('abc') or 0 / 0. NaN is the only JavaScript value that is not equal to itself under == or ===.
const result = Number('abc');
console.log(result === NaN); // false, always
console.log(result !== result); // true: only NaN does this
console.log(Number.isNaN(result)); // true
console.log(isNaN('abc')); // true: global isNaN converts first
console.log(Number.isNaN('abc')); // false: 'abc' is a string, not NaN
console.log([NaN].indexOf(NaN)); // -1 (strict equality)
console.log([NaN].includes(NaN)); // true (SameValueZero)-0: a zero with a sign
JavaScript numbers follow the IEEE 754 standard, which has both +0 and -0. You can get -0 from expressions like -1 * 0 or Math.round(-0.4). Strict equality treats the two zeros as equal, and so does SameValueZero. Only Object.is tells them apart.
const z = Math.round(-0.4);
console.log(z === 0); // true
console.log(Object.is(z, -0)); // true
console.log(String(z)); // '0': the sign disappears in strings
console.log(1 / z); // -Infinity: the sign shows up hereThe sign matters when it carries meaning, for example the direction of a value that rounded to zero. Printing hides it, so Object.is is the reliable way to check for it.
null and undefined: a closed pair under ==
Loose equality has a special rule here. null and undefined are loosely equal to each other and to nothing else. Neither one is converted to a number for ==.
console.log(null == undefined); // true
console.log(null == 0); // false
console.log(undefined == 0); // false
console.log(null == false); // false
console.log(undefined == ''); // false
function isMissing(value) {
return value == null; // true only for null or undefined
}
console.log(isMissing(0), isMissing(undefined)); // false truevalue == null is the one place where many style guides allow ==. It works the same way as value === null || value === undefined.
Summary table
| Expression | `===` | `==` | `Object.is` |
|---|---|---|---|
| `NaN` vs `NaN` | false | false | true |
| `0` vs `-0` | true | true | false |
| `null` vs `undefined` | false | true | false |
| `null` vs `0` | false | false | false |
Exercise
What does each line print? console.log([0].includes(-0)); console.log(undefined == null); console.log(Number.isNaN(undefined)); console.log(undefined === null);
Show answer
true (SameValueZero treats 0 and -0 as equal), true (null and undefined are loosely equal), false (undefined is not NaN, though Number(undefined) is), false (they are different types).
Exercise
A function receives x = 0 / 0. Why does if (x === NaN) never run its body, and what should you write instead?
Show answer
NaN is not equal to anything under ===, including itself, so the condition is always false. Write if (Number.isNaN(x)), or if (Object.is(x, NaN)).