JavaScript parseInt() function is “used to parse a string argument and returns an integer of the specified radix”. The radix is the base number used in mathematical systems.
To convert a string to integer in JavaScript, you can use the “parseInt()” function.
Syntax
parseInt(string, radix);
Parameters
- string: It is an argument that needs to be converted into an integer.
- radix: It is an integer between 2 and 36 that represents the radix which is the base in mathematical numeral systems of the string mentioned above.
Return value
It returns an integer parsed from the given string, or NaN, when,
- The radix as a 32-bit integer is smaller than 2 or bigger than 36
- The first non-whitespace character cannot be converted to a number.
Example 1: How to Use parseInt() Method
let a = parseInt('21');
console.log(a);
Output
The parseInt() function converts its first argument to the string, parses it, and returns an integer or NaN. If not NaN, then the returned value would be an integer.
Example 2: Use parseInt() with different type of arguments
console.log(parseInt('0xF', 16));
console.log(parseInt('F', 16));
console.log(parseInt('17', 8));
console.log(parseInt(021, 8));
console.log(parseInt(15.99, 10));
console.log(parseInt('15,123', 10));
console.log(parseInt('FXX123', 16));
console.log(parseInt('1111', 2));
console.log(parseInt('15 * 3', 10));
console.log(parseInt('15e2', 10));
console.log(parseInt('15px', 10));
console.log(parseInt('12', 13));
Output
But if the string does not start with a number, you’ll get NaN (Not a Number).
console.log(parseInt("I am 21", 10));
Output
The Radix parameter is used to specify which numerical system will be used. For example, the radix of 16 (hexadecimal) indicates that a number in a string should be parsed from the hexadecimal number to the decimal number.
If a radix parameter is not specified, the JavaScript assumes the following.
- If the string begins with ‘0x’, the radix is 16 (hexadecimal).
- If the string begins with ‘0’, the radix is 8 (octal). This feature is deprecated.
- If a string begins with any other value, the radix is 10 (decimal).
Example 3: Using parseInt() on non-strings
The parseInt() function can have interesting results when working on non-strings combined with a high radix.
console.log(parseInt(null, 36));
console.log(undefined, 36);
Output
1112745
undefined 36
Browser Compatibility
- Google Chrome 1 and above
- Edge 12 and above
- Firefox 1 and above
- Safari 1 and above
- Opera 3 and above
That’s it.

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.