The Wayback Machine - https://web.archive.org/web/20240128193854/https://www.geeksforgeeks.org/javascript-number-parseint-method/
Open In App
Related Articles

JavaScript Number parseInt() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

JavaScript parseInt() Method is used to accept the string and radix parameter and convert it into an integer. The radix parameter is used to specify which numeral system to use, for example, a radix of 16 (hexadecimal) indicates that the number in the string should be parsed from a hexadecimal number to a decimal number. If the string does not contain a numeric value then it returns NaN i.e., not a number. 

Note: This method is deprecated.

Syntax:

parseInt(Value, radix);

Parameters:

  • Value: This parameter contains a string that is converted to an integer.
  • radix: This parameter represents the radix or base to be used and it is optional.

Return value:

It returns a number and if the first character can’t be converted to a number then the function returns NaN. It returns a number parsed up to that point where it encounters a character that is not a number in the specified radix(base). 

Example 1: In this example, we are using the parseInt() method to parse the given float value.

javascript




let v1 = parseInt("3.14");
console.log('Using parseInt("3.14") = '+ v1);


Output

Using parseInt("3.14") = 3

Example 2: In this example, we are using the parseInt() method to parse the given values.

javascript




a = parseInt("100");
console.log('parseInt("100") = ' +
    a);
 
// It returns a Integer until
// it encounters Not a Number character
b = parseInt("2018@geeksforgeeks");
console.log('parseInt("2018@geeksforgeeks") = ' +
    b);
 
// It returns NaN on Non numeral character
c = parseInt("geeksforgeeks@2018");
console.log('parseInt("geeksforgeeks@2018") = ' +
    c);
 
// It returns Integer value of a Floating point Number
d = parseInt("3.14");
console.log('parseInt("3.14") = ' + d);
 
// It returns only first Number it encounters
e = parseInt("21 7 2018");
console.log('parseInt("21 7 2018") = ' +
    e);


Output

parseInt("100") = 100
parseInt("2018@geeksforgeeks") = 2018
parseInt("geeksforgeeks@2018") = NaN
parseInt("3.14") = 3
parseInt("21 7 2018") = 21

Example 3: In this example, we will also mention radix with the number.

javascript




// Base 10
a = parseInt("100", 10);
console.log('parseInt("100",10) = ' +
    a);
 
// Base 8
b = parseInt("8", 8);
console.log('parseInt("8",8) = ' +
    b);
 
// Base 8
c = parseInt("15", 8);
console.log('parseInt("15",8) = ' +
    c);
 
// Base 16
d = parseInt("16", 16);
console.log('parseInt("16",16) = ' +
    d);
 
// Leading and trailing spaces are ignored
// in parseInt() function
e = parseInt(" 100 ");
console.log('parseInt(" 100 ") = ' +
    e);
 
// Base 16(hexadecimal)
f = parseInt("0x16");
console.log('parseInt("0x16") = ' +
    f);


Output

parseInt("100",10) = 100
parseInt("8",8) = NaN
parseInt("15",8) = 13
parseInt("16",16) = 22
parseInt(" 100 ") = 100
parseInt("0x16") = 22

Supported Browsers

  • Google Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Safari 1 and above
  • Opera 3 and above

We have a complete list of JavaScript Number constructor, properties, and methods list, to know more about the numbers please go through that article.


Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!


Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 13 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials