Number.parseInt()

Identical to the global parseInt — the same function object, re-exposed on Number in ES2015. Its lenient parsing is useful for things like "42px" and disastrous everywhere else.

Number static methodES2015 (global since ES1)Live demo
Common call
parseInt(s, 10)
Returns
an integer, or NaN
Replaces
nothing; Number(s) is the strict alternative
Watch out
never pass it directly to map — the index becomes the radix
Number.parseInt(string[, radix])
→ number

Demo

Live evaluation
Try:
Inputs
sstringtext to parse
radixnumberbase (10 for decimal)
Output
parseInt('42px', 10)
42

Parsing runs from the left and stops at the first character that is not a digit in the given base, so '42px' gives 42 while 'px42' gives NaN — leading junk is fatal, trailing junk is ignored. The fourth case catches people out: '1e3' parses as 1, because 'e' is not a decimal digit and parsing halts there. Number('1e3') gives 1000, because it understands exponent notation. That difference alone decides which of the two you should be using.

Parameters

NameTypeRequiredDescription
stringstringyesThe text to parse. Leading whitespace is skipped; parsing stops at the first invalid character. A non-string argument is converted to a string FIRST, which is how tiny numbers parse wrongly.
radixnumberno (10)The base, 2 to 36. Omitted it is 10, except that a 0x prefix forces 16. Always pass it explicitly.

Return value

number — An integer parsed from the START of the string, stopping at the first character that is not a valid digit. NaN if nothing valid was found at all.

Common patterns

Always pass the radix
Removes the 0x special case entirely.
const n = parseInt(input, 10);
In a map, wrap it
Otherwise the index arrives as the radix.
strings.map(s => parseInt(s, 10));
Use Number for strict parsing
Rejects trailing junk instead of ignoring it.
const n = Number(input);   // NaN for "42px"

Examples

1. Trailing junk ignored
parseInt('42px', 10)
Returns
42
2. Leading junk fatal
parseInt('px42', 10)
Returns
NaN
3. Exponent not understood
parseInt('1e3', 10)
Returns
1
4. Number understands it
Number('1e3')
Returns
1000
5. The map trap
['10', '10', '10'].map(parseInt)
Returns
[10, NaN, 2]
6. Tiny numbers break
parseInt(0.0000005)
Returns
5

Pitfalls

1. Passing it directly to map
map calls its callback with (value, index, array), so the index becomes the radix. The first element parses in base 0 — treated as 10 — the second in base 1, which is invalid and gives NaN, and the third in base 2. The result looks random and is entirely deterministic.
Index as radix
['10', '10', '10'].map(parseInt)
[10, NaN, 2]
Wrap it
['10', '10', '10'].map(s => parseInt(s, 10))
[10, 10, 10]
2. It converts non-strings to strings first
Passing a number looks harmless until the number is small enough to stringify in exponential form. 0.0000005 becomes "5e-7", parsing stops at the e, and the answer is 5 — off by seven orders of magnitude, silently.
Wildly wrong
parseInt(0.0000005)
5
Truncate properly
Math.trunc(0.0000005)
0
3. Trailing garbage is accepted
It is a lenient parser by design, which makes it unsuitable for validation — "42abc" and "42" both give 42, so a form field full of nonsense passes silently. Number is strict and returns NaN for anything not entirely numeric.
Accepts junk
parseInt('42abc', 10)
42
Strict
Number('42abc')
NaN
4. Omitting the radix still has one special case
Modern engines no longer treat a leading zero as octal, so parseInt("08") is 8. But a 0x prefix is still read as hexadecimal, so a string of digits beginning 0x parses in base 16 when you expected base 10.
Hex assumed
parseInt('0x1f')
31
Force base 10
parseInt('0x1f', 10)
0

When to use

Use it
  • Pulling a number off the front of text — "42px", "3 items"
  • Parsing in a non-decimal base, with an explicit radix
  • Reading values from formats that append units
Reach for something else
  • Validating user input → Number, which rejects trailing junk
  • Values in exponent notation → Number
  • Truncating a number you already have → Math.trunc
  • Decimals matter → parseFloat

Notes

Complexity
O(n) in the length of the parsed prefix
Return
A number, or NaN; nothing is modified
CPython impl
V8: Builtins-number-parseint
Memory
May allocate when converting a non-string argument
Thread-safe
Single-threaded

FAQ

Number for validation — it rejects anything not entirely numeric and understands exponent notation. parseInt when you deliberately want the leading digits of a string that has other characters after them, like a CSS length.

Number('42px');        // NaN — strict
parseInt('42px', 10);  // 42  — lenient

History

ES1
parseInt present as a global from the first version, with octal-by-leading-zero behaviour.
ES5
The implicit octal interpretation removed; only the 0x prefix remains special.
ES2015
Exposed as Number.parseInt, the identical function under a namespace.