JavaScript parseInt()
1. Introduction
This is an in-depth article related to the Javascript parseInt method. This method is used for parsing an integer string to return an integer. This method has parameters string and radix. Radix parameter represents the numeral system.
2. Javascript parseInt()
The parseInt() method parses a string and returns an integer or NaN.
2.1 Prerequisites
A browser that supports javascript is needed to run this example.
2.2 Download
You can download any browser which supports javascript. The browsers that support the JavaScript Array parseInt() method are mentioned below:
- Google Chrome 1 and above
- Edge 12 and above
- Firefox 1 and above
- Internet Explorer 3 and above
- Safari 1 and above
- Opera 3 and above
2.3 Javascript parseInt() Syntax
The syntax of the parseInt() method is shown below:
parseInt syntax
parseInt(Value, radix)
2.4 Parameters
parseInt method takes two parameters value and radix. Value is the string which can be changed to an integer. radix is the numerical system base. radix is the optional parameter. This method returns an integer. If the value string cannot be converted to an integer, NaN (Not a Number) is returned.
2.5 Simple Example without Radix
Now let us look at the javascript parseInt method usage without Radix. You can see the code below demonstrating the example.
parseInt example without Radix
const exInt = '45'; console.log(3 + exInt); console.log(3 + parseInt(exInt));
You can execute the code above in a browser that supports javascript. The output in the browser console will be as below:
Simple Example Output
345 48
2.6 Example with Radix
Now let us look at the example of the method with radix.
parseInt example with Radix
console.log(parseInt("60", 18));
You can execute the code above in a browser that supports javascript. The output in the browser console will be as below:
Radix Example Output
108
3. Conclusion
The javascript parseInt function converts its first argument to a string, parses that string, then returns an integer or NaN. If not NaN, the return value will be the integer that is the first argument taken as a number in the specified radix. (For example, a radix of 10 converts from a decimal number, 8 converts from octal, 16 from hexadecimal, and so on.)
4. Download the Source Code
You can download the full source code of this example here: JavaScript parseInt()