liveBook · Manning

Number conversion

As explained in JavaScript in Depth

The language process that converts a value to a numeric value according to coercion rules.

A numeric-looking value can hide a performance bug or a sorting failure. JavaScript silently converts operands during arithmetic, so a comparator such as a - b may work for numbers, run several times slower for numeric strings, and treat ISO date strings as equal because each subtraction produces NaN. section 4.4.1 section 4.4.4

Number conversion follows predictable rules, including several edge cases:

  • Number(false) and Number(null) produce 0; Number(true) produces 1. section 4.4.3
  • Numeric strings become numbers: Number('42') is 42 and Number('3.14') is 3.14. section 4.4.3
  • Empty or whitespace-only strings become 0, while undefined and nonnumeric strings become NaN. section 4.4.3
  • Arrays pass through string conversion first: [] becomes 0, [5] becomes 5, and [1,2] becomes NaN. section 4.4.3 section 4.4.4

Arithmetic operators, comparisons, and built-in functions can trigger conversion automatically. In a sort comparator, a - b converts both operands on every comparison rather than once per element. The result remains O(n log n), but repeated conversions create a substantial constant cost that grows with comparison count. section 4.4.3 section 4.4.4

A comparator that silently stops sorting

const timestamps = ['1861-03-04T17:00:00Z', '2009-01-20T17:00:00Z', '2021-01-20T17:00:00Z']; timestamps.sort((a, b) => a - b); Since ISO date-times cannot become numbers, every comparison returns NaN. Sorting treats that result as +0, so the elements compare equal and remain in arrival order. Convert or validate the values before sorting instead of relying on implicit arithmetic conversion. section 4.4.1

console.log(Number([1]));  // 1
console.log(Number([1,2]));  // NaN

Compare Number([1]) with Number([1,2]) to expose the array-to-string-to-number conversion chain. section 4.4.4

const timestamps = ['-3434252400', '1232470800', '1611162000'];
timestamps.sort((a, b) => a - b);

Run numeric-string sorting beside numeric sorting to observe correct results with additional conversion work. section 4.4.1

How JavaScript in Depth covers it

How deep it goes
Works through primitive and array cases, comparator behavior, NaN, and repeated conversion costs without detailing engine internals.
Examples
Uses timestamp sorting, numeric strings, ISO date-times, array conversion, and the surprising expression [] == false.
What it assumes
Assumes basic JavaScript operators and arrays while teaching conversion rules and coercion chains directly.
What you can do after
Diagnose incorrect sorts and hidden conversion costs, then validate or normalize values before repeated numeric operations.
How it connects
Connects number conversion to type coercion, boolean conversion, string conversion, equality, arrays, NaN, sorting, and TypeScript.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.