- Instant help with your JavaScript coding problems

Convert string to int in JavaScript

How to convert string to int in JavaScript?
let str = '421';
let num = parseInt(str);

// Converting float
let piStr = '3.14';
let pi = parseFloat(piStr);

Converting a string value to a numeric value in JavaScript is quite common task. For example HTML input values are always in string format so for numeric inputs you need to convert that string to in ro float.

To do this use the parseInt function that parses a string argument and returns an integer. Alternatively if you want to convert a string to float use parseFloat function that parses an argument (converting it to a string first if needed) and returns a floating point number.

parseInt reference
Share "How to convert string to int in JavaScript?"
Related snippets:
string to int, string to number, string to float, string to integer
Technical term:
Convert string to int in JavaScript