JavaScript Number parseInt() method

The parseInt function in JavaScript is utilized to analyze a given string and transform it into an integer value. When passing a string as an argument, there is also the option to include a radix argument, which allows us to define the numeral system that should be applied.

Syntax

The syntax for the parseInt function is expressed as follows:

Example

Number.parseInt(string, radix)

Parameter

string - It represents the string to be parsed.

radix - This parameter is optional. It is an integer value ranging from 2 to 36 that signifies the numeral system that will be utilized.

Return

An integer value. It produces NaN if the initial character is not convertible into a numerical format.

JavaScript Number parseInt method example

In this section, we will explore the parseInt function using a range of examples.

Example 1

Let's see a simple example of parseInt method.

Example

<script>

var a="50";

var b="50.25"

var c="String";

var d="50String";

var e="50.25String"

document.writeln(Number.parseInt(a)+"<br>");

document.writeln(Number.parseInt(b)+"<br>");

document.writeln(Number.parseInt(c)+"<br>");

document.writeln(Number.parseInt(d)+"<br>");

document.writeln(Number.parseInt(e)); 

</script>

Output:

Output

50

50

NaN

50

50

Example 2

Let's examine an illustration of how to concatenate two strings, both by utilizing the parseInt method and by not using it.

Example

<script>

var a="10";

var b="20";

var c=a+b;

document.writeln("Before invoking parseInt(): "+c+"<br>");

var c=Number.parseInt(a)+Number.parseInt(b);

document.writeln("After invoking parseInt(): "+c);

</script>

Output:

Output

Before invoking parseInt(): 1020

After invoking parseInt(): 30

Example 3

In this illustration, we will provide the radix parameter to the parseInt function.

Example

<script>

var a="50";

document.writeln(Number.parseInt(a,10)+"<br>");

document.writeln(Number.parseInt(a,8)+"<br>");

document.writeln(Number.parseInt(a,16));

</script>

Output:

Output

50

40

80

Input Required

This code uses input(). Please provide values below: