Convert a String to an Integer in JavaScript

parseInt()

parseInt() function to convert a string to an integer

 

console.log(parseInt('37', 10)); // 37
console.log(parseInt('37.5', 10)); // 37
console.log(parseInt('-37', 10)); // -37
console.log(parseInt('test', 10)); // NaN
console.log(parseInt('123test', 10)); // 123

 

parseInt() function to parse a string to an integer. This function takes 2 parameters, first string we want to parse to an integer and The second parameter the function takes is the base, and should be set to 10


Tags:

Share:

Related posts