- Instant help with your JavaScript coding problems

Convert string to int in JavaScript

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

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

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.

Reference:
parseInt reference
Share "How to convert string to int in JavaScript?"