To convert a String to an Integer in JavaScript, you can use the parseInt()
function.
In this example,
str
with a string value of "123"
.parseInt()
function to convert it to an integer and store the result in the variable number
.parseInt()
function parses the string and returns the equivalent integer value.number
to the console.let str = "123";
let number = parseInt(str);
console.log("Integer value: " + number);
Integer value: 123
In this example,
str
with a string value of "456"
.parseInt()
function to convert it to an integer and store the result in the variable number
.parseInt()
function parses the string and returns the equivalent integer value.number
to the console.let str = "456";
let number = parseInt(str);
console.log("Integer value: " + number);
Integer value: 456
In this tutorial, we learned How to Convert String to Integer in JavaScript language with well detailed examples.