JavaScript Code

How to convert string to integer?


Problem Statement

given a string value, convert the string to integer

Solution

to convert a given string into integer, use JavaScript global function parseInt(). pass the string as argument to parseInt() function, and the function returns the integer value

Program

str is the given string value. we use parseInt() method to parse the string str into an integer

let str = "45";
let num = parseInt(str);
console.log(num);
console.log(typeof(num));