How to Convert JavaScript String to Int

To convert String to int in JavaScript, use the parseInt() function. The parseInt() is a built-in JavaScript that parses a string argument and returns an integer of the specified radix. If the string contains any alphabet, it stops parsing at that moment or parses only integer before the alphabets.


parseint(string, radix)


string: It is the input value, which contains an integer and is to be converted.

radix: It is on the other side specifies which number system is being used in terms of computer, For example, 2-Binary, 8=octal, etc., till 36.


let x = '5helloworld';
let z = '111';
console.log(parseInt(z , 2));



You can see that the parseInt() function has converted a 5helloworld” string into an integer value of 5.

Implementing radix = 2 takes all the input in binary digits like here 111 is converted into 7. By default, the numbers are converted into decimal values. 

Now a question might arise, what if we specify radix as 10 only. We can see that by taking another example.


let data = '12';
console.log(parseInt(data, 10));



You can see that we get a decimal value only in the output.

How does the parseInt() function works

The parseInt() function checks for all the numerical values on the left of any alphabet then parses the value into an integer, and that’s how “5helloworld” was converted into 5 as the output.

let y = "-9"
let u = "hello"
let m = ”9+8”



We can see that the sign symbol is also parsed in the output, and if an alphabetical string is input, NaN(Not a Number) is returned. 

If you encounter a string with a value having a number on the right side of the alphabet, a big problem can arise as only numbers on the left will be printed.

Also, we used + sign in the third example, but parseInt stopped the reading process and resulted in only showing 9 as the output.

let data = "6Javascript is fun but by parse int only numbers before any alphabet will parse 7"



Here, only 6 is shown as the output, and 7 has been ignored.

Converting string to an integer using Number object

The Number object in JavaScript is used to convert any Boolean, dates, strings value into a number, or we can also say it converts them into integers. 


Number (value);


The Number() function accepts a value as an argument.


Let’s convert string to int using the Number() function.

let data = "88"



Number constants

  1. MIN_VALUE – It gives us the minimum value among a set of numbers.
  2. MAX_VALUE – It gives us the maximum value among a set of numbers.
  3. POSITIVE_INFINITY – It gives us the positive infinity when a number overflow occurs.
  4. NEGATIVE_INFINITY – It gives us the negative infinity when a number overflow occurs.
  5. NAN – It gives us the Not a Number value when a pure string of alphabets is used instead of integer values.


With the help of this easy syntax and examples, I hope that you would have understood how to convert a string to int in JavaScript and how we can use redix parameters in the parseInt while also exploring different Constants in Number object.

That’s it for this tutorial.

See also

JavaScript String to Boolean

JavaScript Array to String

Leave a Comment