NaN and isNaN Operator
NaN, short for “not a number”, is the value JavaScript gives when a calculation or conversion cannot produce a real number. Despite the name, it belongs to the number type. Checking for it needs care, because NaN is not equal to anything, itself included.
Where NaN comes from
Section titled “Where NaN comes from”Converting text that does not read as a number gives NaN, and so do undefined maths operations such as 0 / 0.
console.log(Number('twelve'));console.log(0 / 0);console.log(Math.sqrt(-4));console.log(parseInt('kg 5'));console.log(typeof NaN);This prints:
NaNNaNNaNNaNnumberOnce NaN enters a calculation, it spreads, so any arithmetic with it gives NaN, and every comparison with it is false.
const temperatures = [14, NaN, 17];const total = temperatures.reduce((sum, t) => sum + t, 0);console.log(total);console.log(NaN > 0, NaN < 0, NaN == 0);This prints:
NaNfalse false falseNaN is not equal to itself
Section titled “NaN is not equal to itself”NaN === NaN is false, so an equality test cannot find it. The same goes for indexOf, which uses strict equality. includes and Object.is use a comparison that treats NaN as equal to itself, so they do find it.
const reading = Number('n/a');
console.log(reading === NaN);console.log(reading !== reading);console.log([NaN].indexOf(NaN));console.log([NaN].includes(NaN));console.log(Object.is(reading, NaN));This prints:
falsetrue-1truetrueNaN is the only value for which x !== x is true, which is how older code tested for it.
isNaN and Number.isNaN
Section titled “isNaN and Number.isNaN”There are two functions with similar names, and they answer different questions:
isNaN(value), the original global function, converts the value to a number first and then checks the result. It answers “would this becomeNaNif converted?”.Number.isNaN(value), added in ES2015, does no conversion and returnstrueonly when the value isNaNitself.
const inputs = ['42', 'abc', '', ' ', true, null, undefined, [], {}];
for (const value of inputs) { console.log(JSON.stringify(value) ?? String(value), isNaN(value), Number.isNaN(value));}This prints:
"42" false false"abc" true false"" false false" " false falsetrue false falsenull false falseundefined true false[] false false{} true falseThe global isNaN gives some surprising answers. An empty or blank string converts to 0, so it counts as a number, and null, true and [] do too. Prefer Number.isNaN for checking a value you already hold as a number, and use Number.isFinite when Infinity should be rejected as well.
Validating input
Section titled “Validating input”To check that a piece of text is a number, convert it explicitly and test the result with Number.isNaN. The blank-string case needs its own check, because Number('') and Number(' ') are both 0.
function parseQuantity(text) { const value = Number(text); if (text.trim() === '' || Number.isNaN(value)) { return null; } return value;}
console.log(parseQuantity('3'));console.log(parseQuantity('three'));console.log(parseQuantity(' '));This prints:
3nullnullSee Type Casting, Converting & Coercion for the full rules on converting values to numbers.
Steve Griffith covers this topic in his video NaN and isNaN in JavaScript.