NaN, Infinity and undefined
Three answers instead of a crash
Ask most languages to divide by zero, or to do arithmetic on a word, and the program stops and tells you which line was at fault. JavaScript almost never stops. It hands back a special value and moves on to the next line.There are three of these values: NaN, Infinity and undefined. You have met the first one already, back where Number() was handed text that spelled nothing numeric, and you have seen the second turn up in an overflow. This is where all three get looked at properly. None of them is an error message. Each one is a real value that can be stored in a variable, passed to a function and printed, which is exactly what makes them dangerous. A program that has already gone wrong keeps running, and keeps looking healthy.
NaN
NaN is short for Not a Number. It is what an arithmetic operation gives you when there is no numeric answer to give.Now the strangest fact in the language, and the one rule about NaN you have to memorise: NaN is not equal to itself. Every comparison you make with it comes back false, including a comparison against NaN itself, so the obvious test never works.
There is a second surprise waiting. Ask JavaScript what type NaN is, with
typeof NaN, and the answer is the word number. That is not a bug: NaN is the value the language uses for a numeric answer that does not exist, so it is filed with the numbers. It also means typeof is useless for spotting one.Infinity
Dividing by zero does not stop a JavaScript program either. Neither does asking for a value too large to store.undefined
undefined is JavaScript's way of saying nobody ever put a value here. There are four common ways to meet it.| Where it comes from | Example | What is undefined |
|---|---|---|
| A variable declared but never assigned | let hatch; | hatch |
| A call that leaves out an argument | unpack(20) where unpack takes two | the second parameter |
| A function that never returns anything | let r = polish(); | r |
| An index past the end of a list | [6, 21][9] | the value handed back |
Two of those four involve functions, which get a unit of their own later on. For now you only need the shape:
function name(a, b) { ... } defines one, name(4, 9) calls it, and whatever the function returns is what the call turns into. A function with no return line turns into undefined, however much work it did along the way.Why any of this matters
A crash is loud. It stops the program and names the line. NaN, Infinity and undefined are silent, and they travel.The program below turns the text "52" into a number, cuts a rope of that length into equal pieces, and checks the result with
Number.isNaN. Run it, then make each change below and run it again. Press Reset before each change, so that each one starts from the original program.• Change
"52" to "fifty-two".• Set
pieces to 0.• Change
"52" to "0" and set pieces to 0.let typed = "52";
let rope = Number(typed);
let pieces = 4;
let each = rope / pieces;
console.log("Each piece:", each);
console.log("NaN check:", Number.isNaN(each));
console.log("Longer than 10:", each > 10);
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.
| Value | Where it comes from | typeof says |
|---|---|---|
NaN | arithmetic with no numeric answer | number |
Infinity | dividing by zero, or a value too large to store | number |
undefined | nothing was ever assigned here | undefined |
null | a programmer assigned emptiness on purpose | object, a long-standing quirk of the language |