• JavaScript Video Tutorials

JavaScript Number parseInt() Method



The JavaScript Number parseInt() method is used to convert a string into an integer according to a specified 'radix'. The Radix parameter represents the base in a mathematical numeral system, and must be an integer within the range of 2 to 36, inclusive. If the input string is invalid, or if the specified radix is outside of this range, the method returns 'NaN'.

Please find the important points listed below −

  • If the input string has any leading whitespace, plus or minus signs, they will be removed from the string while converting, and if the string starts with "0x" or "0X", the radix is assumed to be 16, and the rest of the string will be treated as a hexadecimal number and converted accordingly.
  • If the passed string begins with any other value, the radix(base) is 10 (decimal).

Syntax

Following is the syntax of JavaScript Number parseInt() method −

parseInt(string, radix)

Parameters

This method accepts two parameters: a "string" and an optional "radix", which are described below −

  • string − The string start with an intger to be parsed.
  • radix (optional) − It is an integer represents base in mathematical numeral system.

Return value

This method returns an integer value from the passed string.

Example 1

The following example demonstrates the usage of the JavaScript number parseInt() method.

<html>
<head>
<title>JavaScript parseInt() Method</title>
</head>
<body>
<script>
   let val = " 10f ";
   document.write("Given value = " , val)
   document.write("<br>Integer value = ", Number.parseInt(val));
</script>
</body>
</html>

Output

Once the above program is executed, it will return an inetger value '10' −

Given value = 10f
Integer value = 10

Example 2

If the passed radix value is not in the range of [2, 36], it will return 'NaN' in the output.

<html>
<head>
<title>JavaScript parseInt() Method</title>
</head>
<body>
<script>
   let val = "10fcs";
   let radix = 40;
   document.write("Given value = ", val);
   document.write("<br>Radix = ", radix);
   document.write("<br>Integer value = ", Number.parseInt(val, radix));
</script>
</body>
</html>

Output

If we execute the above program, it returns 'NaN'.

Given value = 10fcs
Radix = 40
Integer value = NaN

Example 3

If a string starts with 'white spaces' and possibly 'plus' or 'minus' signs, they will be removed while converting to an integer value, and in this case, the radix is assumed to be 16.

<html>
<head>
<title>JavaScript parseInt() Method</title>
</head>
<body>
<script>
   let val = " +23abc12";
   let radix = 16;
   document.write("Given value = ", val);
   document.write("<br>Radix = ", radix);
   document.write("<br>Integer value = ", Number.parseInt(val, radix));
</script>
</body>
</html>

Output

The above program returns an integer value "37403666" for input string " +23abc12" −

Given value = +23abc12
Radix = 16
Integer value = 37403666

Example 4

Let's test what happens if the input string doesn't start with a valid number, but the radix is within the range of [2, 36].

<html>
<head>
<title>JavaScript parseInt() Method</title>
</head>
<body>
<script>
   let val = "abc123";
   let radix = 8;
   document.write("Given value = ", val)
   document.write("<br>Radix = ", radix);
   document.write("<br>Integer value = ", Number.parseInt(val, radix));
</script>
</body>
</html>

Output

If the input string is invalid, it will return 'NaN' in the output as −

Given value = abc123
Radix = 8
Integer value = NaN
Advertisements