Read integer text
Convert a decimal string such as "42" or a decimal prefix such as "42px" into an integer.
JavaScript · Function
parseInt() converts a value to a string and parses an integer in a specified radix. It returns the parsed integer or NaN when the radix is invalid or no initial numeric characters can be parsed.
parseInt() converts its first argument to a string, ignores leading whitespace and an optional sign, then reads consecutive characters that are valid in the chosen radix. It stops at the first invalid character and returns the accumulated integer; if no valid integer can be read, or the radix is outside 2–36 after conversion, it returns NaN.
parseInt(string)
parseInt(string, radix)| Name | Type | Description |
|---|---|---|
string | string | A value converted to a string and parsed from its beginning as an integer. |
radix | number, optional | An integer base from 2 through 36. If omitted or zero, decimal is used except that a 0x or 0X prefix selects hexadecimal. |
An integer parsed from the string, or NaN if the radix is outside 2–36 after conversion or the first non-whitespace character cannot be converted to a number.
const whole = parseInt("42px", 10);
const hexadecimal = parseInt("ff", 16);
console.log(whole); // 42
console.log(hexadecimal); // 255Parses a decimal prefix and a hexadecimal string using explicit radices.
Convert a decimal string such as "42" or a decimal prefix such as "42px" into an integer.
Interpret binary, octal, hexadecimal, or another base from 2 through 36.
Convert a number represented in one radix before formatting it with Number.prototype.toString().
Number.parseInt()parseFloat()Number()Number.prototype.toString()