Open In App

JavaScript Number parseInt() Method

Last Updated : 05 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The parseInt() method in JavaScript converts a string into an integer (a whole number) based on the specified radix (base). If the first character of the string cannot be converted to a number, NaN is returned.

The method takes two parameters: the string to be parsed and the radix (optional, default is 10).
2 = binary, 8 = octal, 10 = decimal, 16 = hexadecimal.

Number parseInt() Method Syntax

parseInt(Value, radix);

Number parseInt() Method 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.

Number parseInt() Method 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). 

Number parseInt() Method Examples

Example 1: Parsing float value

Here, 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


Explanation

  • In the above example we parseInt(“3.14”) converts the string “3.14” to an integer.
  • Since “3.14” is parsed, only the integer part (3) is considered.
  • The value of v1 will be 3, not 3.14.

Example 2: Parsing value with given radix

Here, 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


Explanation:

The code demonstrates the parseInt() method with different radix values and string formats:

  • "100" parsed in base 10 yields 100.
  • "8" parsed in base 8 yields 8.
  • "15" parsed in base 8 (treated as octal) yields 13.
  • "16" parsed in base 16 (hexadecimal) yields 22.
  • Leading and trailing spaces are ignored; " 100 " yields 100.
  • "0x16" is treated as hexadecimal and yields 22.

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

Supported Browsers



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads