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.
- 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
- 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
- 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.
- Number.parseInt in the ECMAScript standard
- parseFloat for non-integer parsing
- Alternatives like Number() or unary plus for general numeric conversion