JavaScript parseInt() Method

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.


parseInt(string, radix);


  1. string:  It is an argument that needs to be converted into an integer.
  2. 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,

  1. The radix as a 32-bit integer is smaller than 2 or bigger than 36
  2. The first non-whitespace character cannot be converted to a number.

Example 1: How to Use parseInt() Method

let a = parseInt('21');



Javascript ParseInt Example | parseInt() Function Tutorial

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));


Javascript ParseInt Example

But if the string does not start with a number, you’ll get NaN (Not a Number).

console.log(parseInt("I am 21", 10));



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);


undefined 36

Browser Compatibility

  1. Google Chrome 1 and above
  2. Edge 12 and above
  3. Firefox 1 and above
  4. Safari 1 and above
  5. Opera 3 and above

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.