parseInt()
parseInt(string, radix) Returns:
number · Added in vES1 · Updated March 13, 2026 · Global Functions javascript parseint global conversion parsing
The parseInt() function parses a string argument and returns an integer of the specified radix (base). It is commonly used to convert string representations of numbers into actual numeric values.
Syntax
parseInt(string)
parseInt(string, radix)
Parameters
| Parameter | Type | Description |
|---|---|---|
string | string | The value to parse. If not a string, it is converted to one first. Leading whitespace is ignored. |
radix | number | An integer between 2 and 36 representing the base of the number system. Default is 10. |
Return Value
The parsed integer. If the first character cannot be converted, NaN is returned.
Examples
Basic Integer Parsing
parseInt("42");
// 42
parseInt(" 123 ");
// 123
parseInt("42px");
// 42 (parses until invalid character)
Parsing with Different Radices
// Binary (base 2)
parseInt("1010", 2);
// 10
// Octal (base 8)
parseInt("777", 8);
// 511
// Hexadecimal (base 16)
parseInt("ff", 16);
// 255
Common Pitfalls
// Leading zeros do not mean octal in modern JS (unless radix is 8)
parseInt("010");
// 10
// But with explicit radix 8:
parseInt("010", 8);
// 8
// Floating-point numbers are truncated
parseInt("3.14");
// 3
Invalid Input
parseInt("hello");
// NaN
parseInt("");
// NaN (unlike Number() which returns 0)
When to Use parseInt()
This function is useful when you need to:
- Convert user input strings to numbers
- Parse CSS pixel values (e.g., “100px”)
- Read numeric data from external sources that use specific radix formats
Differences from Number()
Unlike the Number() constructor, parseInt() has distinct behavior:
Number("42px");
// NaN (the entire string must be a valid number)
parseInt("42px");
// 42 (parses until it hits an invalid character)
Number("010");
// 10 (leading zeros are ignored)
parseInt("010");
// 10 (same, but radix matters)
Radix Best Practices
Always specify the radix when parsing:
// Explicit radix prevents unexpected behavior
parseInt("10", 10);
// 10
// Without radix, some implementations may guess incorrectly
parseInt("010");
// 10 (but could be 8 in older environments)
See Also
- JSON.parse() — Parse JSON strings into JavaScript values
- Number.isNaN() — Check if a value is NaN with stricter semantics
- eval() — Evaluate JavaScript code from a string