JavaScript

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

Download
You can download the full source code of this example here: JavaScript parseInt()

Bhagvan Kommadi

Bhagvan Kommadi is the Founder of Architect Corner & has around 20 years’ experience in the industry, ranging from large scale enterprise development to helping incubate software product start-ups. He has done Masters in Industrial Systems Engineering at Georgia Institute of Technology (1997) and Bachelors in Aerospace Engineering from Indian Institute of Technology, Madras (1993). He is member of IFX forum,Oracle JCP and participant in Java Community Process. He founded Quantica Computacao, the first quantum computing startup in India. Markets and Markets have positioned Quantica Computacao in ‘Emerging Companies’ section of Quantum Computing quadrants. Bhagvan has engineered and developed simulators and tools in the area of quantum technology using IBM Q, Microsoft Q# and Google QScript. He has reviewed the Manning book titled : "Machine Learning with TensorFlow”. He is also the author of Packt Publishing book - "Hands-On Data Structures and Algorithms with Go".He is member of IFX forum,Oracle JCP and participant in Java Community Process. He is member of the MIT Technology Review Global Panel.
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button