parseInt()

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

Since ES1 Spec ↗

Syntax

parseInt(string, radix)

Returns

number — The parsed integer, or `NaN` if no digits could be parsed.

Examples

console.log(parseInt("42px"));
console.log(parseInt("  10  "));
Output
42
10
console.log(parseInt("ff", 16));
console.log(parseInt("101", 2));
Output
255
5
console.log(parseInt("abc"));
Output
NaN

Notes

- Always pass the radix; without it, leading `0x` is treated as hex. - Parsing stops at the first character that is not a valid digit. - For strict numeric conversion use `Number()` instead.

Browser & runtime support

EnvironmentSince version
chrome 1.0
firefox 1.0
safari 1.0
edge 12
node 0.10

See also