Home

parseint

ParseInt is a JavaScript global function that converts a string to an integer. It accepts two arguments: the value to parse and an optional radix (base) for the numeric interpretation. In modern code, you may also see Number.parseInt, which is the same function.

Behavior and rules

- The function first converts the input to a string (via ToString) and ignores leading whitespace.

- A leading plus or minus sign is allowed.

- If the radix is provided and is between 2 and 36, the string is parsed in that

- If the radix is omitted or zero, the function autodetects the base: strings starting with 0x or

- Parsing stops at the first character that is not valid for the selected base. Any trailing characters

- If no valid digits are found after an optional sign, the result is NaN. If the input

Examples

- parseInt("42") -> 42

- parseInt(" -9px") -> -9

- parseInt("0xF", 16) -> 15

- parseInt("0xF") -> 15 (autodetects hex)

- parseInt("101", 2) -> 5

- parseInt("abc") -> NaN

- parseInt("123.45") -> 123

Radix notes

- The allowed radix range is 2 to 36. Radix values outside this range or non-numeric radix

- Using an explicit radix helps avoid surprises from autodetection, especially with strings that begin with 0.

See also

- Number.parseInt in the ECMAScript standard

- parseFloat for non-integer parsing

- Alternatives like Number() or unary plus for general numeric conversion

base,
using
digits
0–9
and
letters
a–z
(case-insensitive)
as
needed.
0X
are
parsed
as
hexadecimal
(base
16);
otherwise
the
string
is
parsed
as
decimal
(base
10).
are
ignored.
is
a
numeric
value,
it
is
coerced
to
a
string
first;
for
example,
parseInt(12.34)
yields
12.
produce
NaN.