liveBook · Manning

Nan

As explained in Essential TypeScript 5, Third Edition

NaN is the special JavaScript numeric value representing an undefined or unrepresentable numerical result.

NaN means “not a number,” but its JavaScript type is number. It can appear when an arithmetic operation receives a value that cannot produce a useful numeric result. Checking only typeof will not distinguish NaN from an ordinary number. section 3.3.3

A missing argument

In index.js, `function sumPrices(first, second, third) { return first + second + third; }` returns `600` for `sumPrices(100, 200, 300)`, but `sumPrices(100, 200)` returns `NaN`. The missing third argument is undefined; addition with it produces NaN. Both results have type `number`. section 3.3.3

A default parameter addresses that specific mismatch: setting `third = 0` makes the two-argument call return `300`. It does not make every input numeric: passing a string to the addition function can produce concatenated text instead. section 3.3.3

Listing 3.18 Converting and filtering parameters in the index.js file in the primer folder
let hatPrice = 100;
console.log(`Hat price: ${hatPrice}`);
let bootsPrice = "100";
console.log(`Boots price: ${bootsPrice}`);

function sumPrices(...numbers) {
    return numbers.reduce(function(total, val) {
        return total + (Number.isNaN(Number(val)) ? 0 : Number(val));
    }, 0);
}

let totalPrice = sumPrices(hatPrice, bootsPrice);
console.log(`Total: ${totalPrice} ${typeof totalPrice}`);

totalPrice = sumPrices(100, 200, 300);
console.log(`Total: ${totalPrice} ${typeof totalPrice}`);

totalPrice = sumPrices(100, 200, undefined, false, "hello");
console.log(`Total: ${totalPrice} ${typeof totalPrice}`);

This index.js listing takes a variable number of arguments, converts each with `Number`, and uses `Number.isNaN` to substitute zero when conversion produces NaN. Its calls include numeric text, undefined, a boolean, and nonnumeric text. section 3.3.3

A function's unstated assumption

In index.ts, `calculateTax(amount)` multiplies its argument by `1.2`. A number gives a numeric result, while `calculateTax("Hello")` gives NaN. The function assumes a numeric argument, but its untyped parameter can receive a string. section 7.2

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Explains NaN through function outputs and shows ways to prevent or filter it, without examining numeric internals.
Examples
Uses price summation in index.js and tax calculation in index.ts to contrast valid numbers, concatenated strings, and NaN.
What it assumes
Builds on basic function calls and arithmetic while explaining omitted arguments, typeof, and default parameters through examples.
What you can do after
Diagnose/debug NaN results from missing or nonnumeric arguments and apply default values or conversion checks where appropriate.
How it connects
Connects NaN to undefined arguments, type coercion, default parameters, rest parameters, Number.isNaN, and static types.

Also explained in

Compare how 9 books explain nan →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.