Sometimes, we want to remove commas from string using JavaScript.
In this article, we’ll look at how to remove commas from string using JavaScript.
How to remove commas from string using JavaScript?
To remove commas from string using JavaScript, we can use the string replace method.
For instance, we write
const total = parseFloat("100,000.00".replace(/,/g, ""));
to return a string that has the commas removed from "100,000.00" by calling replace on it with the /,/g regex and an empty string.
The g flag searches for all instances of the pattern.
Then we call parseFloat with the returned string to convert the string to a number.
Conclusion
To remove commas from string using JavaScript, we can use the string replace method.