Skip to content

Parsing numbers — parseInt stops at the first bad character, Number() rejects the whole string

Level: 201 · for anyone who turns user input into numbers

Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The questions below are what the finished page has to answer.

One line: parseInt reads leading digits and stops, Number() and unary + want the whole string: parseInt("12px") is 12 but Number("12px") is NaN, Number("") is 0 but parseInt("") is NaN, and parseInt(0.0000005) is 5, because the number first becomes "5e-7".

Keywords: parseInt, parseFloat, radix

What the finished page will answer

  • What do parseInt, parseFloat, Number() and unary + return for "12px", "", " 12 ", "1e3", "0x1f", "0b101" and null?
  • Why is parseInt(0.0000005) equal to 5, and what does tsc say about passing a number to parseInt?
  • What does the radix argument change, what happens with radix 1 or 37, and what does parseInt assume without one?
  • Where do Number() and unary + disagree, and why does +10n throw while Number(10n) is 10?
  • Which spellings does Number() reject that a numeric literal accepts, such as "1_000"?

Examples it will need

  • parsing_numbers_compare_js.js — a table of inputs ("12px", "", " 12 ", "0x1f", "0b101", "1e3", "1_000", null) through parseInt, parseFloat, Number() and unary +
  • parsing_numbers_radix_js.js — parseInt with radix 2, 16, 36, 1 and none, and parseInt(0.0000005) next to String(0.0000005)
  • parsing_numbers_parseint_number_tserror.ts — tsc's TS2345 diagnostic for passing a number to parseInt

See also

Sources to start from