To convert a string
representation of an integer number to a number
type, you can use the parseInt()
function in JavaScript.
Consider this string of integer number,
// string with integer number
const str = "45";
Let's use the parseInt()
function and pass the string
to the function as an argument to convert it to a number
type like this,
// string with integer number
const str = "45";
// convert to number type
const intNum = parseInt(str);
console.log(intNum); // 45
- This function is useful when you want to convert values that are obtained from
url
,form
, or whichever places where the values will bestring
type and need to be converted to an integer number.
See this example live in JSBin.