liveBook · Manning

Number.isNaN

As explained in Essential TypeScript 5, Third Edition

Number.isNaN is a JavaScript method that determines whether a value is the NaN numeric value.

Number.isNaN determines whether a value is the NaN numeric value. In a variadic sum, convert each argument with Number, then replace NaN results with zero before addition. section 3.3.3

Filtering values before summing

function sumPrices(...numbers) { return numbers.reduce(function(total, val) { return total + (Number.isNaN(Number(val)) ? 0 : Number(val)); }, 0); } sumPrices(100, 200, undefined, false, "hello"); // 300 section 3.3.3

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Explains the method through a complete reduce callback and stops at application-level NaN substitution.
Examples
Uses a sumPrices function with numeric values, a numeric string, undefined, false, and a nonnumeric string.
What it assumes
Assumes functions, rest parameters, reduce, and numeric conversion while demonstrating NaN handling within a sum.
What you can do after
Apply it via JavaScript expressions that convert inputs and substitute zero when the result is NaN.
How it connects
Connects Number.isNaN with Number conversion, Array.prototype.reduce, rest parameters, and arrow functions.

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.