Number.parseInt()

Parses a string argument and returns an integer of the specified radix.

Since ES2015 Spec ↗

Syntax

Number.parseInt(string, radix)

Parameters

NameTypeRequiredDescription
string string Yes The value to parse. Leading whitespace is ignored.
radix number No An integer between 2 and 36 giving the base. Strongly recommended to pass 10.

Returns

number — The parsed integer, or NaN if it cannot be parsed.

Examples

console.log(Number.parseInt('42px', 10));
Output
42
console.log(Number.parseInt('0xFF', 16), Number.parseInt('1010', 2));
Output
255 10

Notes

Always pass the radix - without it, leading "0x" is treated as hex. Truncates rather than rounds and stops at the first invalid character. Identical to the global `parseInt`.

Browser & runtime support

EnvironmentSince version
chrome 34
firefox 25
safari 9
edge 12
node 0.12

See also