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








