javascript

Convert a String to Number or Integer in Javascript

const str = "200";
var num = parseInt(str);

If you add a number with a string format integer, it will not perfom sum operation instead it will apppend after the integer which is going to add with it. You need to convert that to integer format to make it functional. You can use parseInt() method to convert a string value to integer or number value. The syntax for that is as below.

Syntax

parseInt(string, radix)

It takes two parameters. First parameter string is required and second parameter radix is optional

string: The string parameter is a required parameter and in this you pass the value that needs to be converted to integer type.

radix: The radix is an optional parameter and it is passed as a number from 2 to 36 to indicate which numeral system it must use.

Live Demo of Integer type conversion

Was this helpful?