String to Number (parseInt)Loading saved progress…

String to Number (parseInt)

parseInt parses an integer out of the beginning of a string: it skips leading whitespace, reads an optional sign, then consumes characters that are valid digits for a chosen base and stops at the first one that is not. That "stop at the first invalid character" behaviour is exactly what separates it from NumberNumber('12px') is NaN, but parseInt('12px') is 12. You will rebuild the global parseInt from scratch as stringToNumber(str, radix), matching its behaviour closely enough that it agrees with the real thing on every input. The one twist worth knowing up front is the base, or radix: it can be given explicitly, defaulted to 10, or inferred as 16 from a leading 0x.

Signature

stringToNumber(str, radix)
// str   — the value to parse (non-strings are coerced with String())
// radix — the base to read in, 2..36; 0 or omitted means base 10 with 0x inference
// returns an integer, or NaN when no digits could be read

Examples

stringToNumber('  -42');   // -42  (leading whitespace skipped, sign read)
stringToNumber('12px');    // 12   (stops at the first non-digit)
stringToNumber('0x1F');    // 31   (0x infers base 16)
stringToNumber('1010', 2); // 10   (explicit binary)
stringToNumber('z', 36);   // 35   (letters are digits in high bases)
stringToNumber('');        // NaN  (no digits at all)

Notes

  • Order matters — parse in this exact order: skip leading whitespace, read one optional + or -, resolve the base, strip an optional 0x, then read digits.
  • Radix rules — coerce the radix with radix | 0. A value of 0 (or omitted) means base 10 with 0x inference; 16 also enables 0x stripping; any other value in 2..36 is used as-is; anything outside 2..36 returns NaN.
  • Digits go past 9 — in bases above 10, letters are digits: a / A is 10 up to z / Z is 35, case-insensitive. A character counts only if its value is below the base.
  • Stop at the first invalid character — unlike Number, you do not reject the whole string; you keep the valid prefix. stringToNumber('100 apples') is 100.
  • Return NaN when nothing was read — an empty string, all whitespace, a lone sign, or a first character that is not a valid digit all yield NaN (not 0).
  • Out of scope — you do not need the C atoi 32-bit clamp, decimal points, or exponents; those are notes in Going further.

FAQ

Why does parseInt stop at the first invalid character instead of returning NaN like Number?
parseInt is a lenient left-to-right scanner: it consumes as many valid leading digits as it can, returns their value, and ignores the rest — so parseInt('12px') is 12. Number is strict; it validates the entire string and returns NaN if any character is invalid, so Number('12px') is NaN.
When the radix is 0 or omitted, how does parseInt pick base 10 versus base 16?
With no radix (or 0) it defaults to base 10 but first peeks at the start of the string: a leading 0x or 0X switches it to base 16 and strips the prefix, so parseInt('0x1F') is 31. Passing an explicit radix of 16 enables the same 0x stripping; any other explicit base disables it, which is why parseInt('0x10', 8) is 0.
What happens if the radix is outside the valid range?
The radix is coerced with ToInt32 (so 16.9 becomes 16 and '8' becomes 8). A result of 0 means default; otherwise it must be between 2 and 36 inclusive. Anything less than 2 or greater than 36 makes parseInt return NaN immediately, before it even looks at the string.
Loading editor…