To remove the commas, you'll need to use replace on the string. To convert to a float so you can do the maths, you'll need parseFloat:

var total = parseFloat('100,000.00'.replace(/,/g, '')) +
            parseFloat('500,000.00'.replace(/,/g, ''));
Answer from lonesomeday on Stack Overflow
Top answer
1 of 3
239

To remove the commas, you'll need to use replace on the string. To convert to a float so you can do the maths, you'll need parseFloat:

var total = parseFloat('100,000.00'.replace(/,/g, '')) +
            parseFloat('500,000.00'.replace(/,/g, ''));
2 of 3
7

Related answer, but if you want to run clean up a user inputting values into a form, here's what you can do:

const numFormatter = new Intl.NumberFormat('en-US', {
  style: "decimal",
  maximumFractionDigits: 2
})

// Good Inputs
parseFloat(numFormatter.format('1234').replace(/,/g,"")) // 1234
parseFloat(numFormatter.format('123').replace(/,/g,"")) // 123

// 3rd decimal place rounds to nearest
parseFloat(numFormatter.format('1234.233').replace(/,/g,"")); // 1234.23
parseFloat(numFormatter.format('1234.239').replace(/,/g,"")); // 1234.24

// Bad Inputs
parseFloat(numFormatter.format('1234.233a').replace(/,/g,"")); // NaN
parseFloat(numFormatter.format('$1234.23').replace(/,/g,"")); // NaN

// Edge Cases
parseFloat(numFormatter.format(true).replace(/,/g,"")) // 1
parseFloat(numFormatter.format(false).replace(/,/g,"")) // 0
parseFloat(numFormatter.format(NaN).replace(/,/g,"")) // NaN

Use the international date local via format. This cleans up any bad inputs, if there is one it returns a string of NaN you can check for. There's no way currently of removing commas as part of the locale (as of 10/12/19), so you can use a regex command to remove commas using replace.

ParseFloat converts the this type definition from string to number

If you use React, this is what your calculate function could look like:

updateCalculationInput = (e) => {
    let value;
    value = numFormatter.format(e.target.value); // 123,456.78 - 3rd decimal rounds to nearest number as expected
    if(value === 'NaN') return; // locale returns string of NaN if fail
    value = value.replace(/,/g, ""); // remove commas
    value = parseFloat(value); // now parse to float should always be clean input

    // Do the actual math and setState calls here
}
🌐
Medium
frontendinterviewquestions.medium.com › how-to-remove-comma-from-string-in-javascript-21c88f166c28
How to remove comma from string in JavaScript | by Pravin M | Medium
March 13, 2024 - let myString = "This, is, a, string, with, commas."; let newString = myString.replace(/,/g, ""); console.log(newString); // Output: This is a string with commas.
Discussions

javascript - Remove last comma (and possible whitespaces after the last comma) from the end of a string - Stack Overflow
Using JavaScript, how can I remove the last comma, but only if the comma is the last character or if there is only white space after the comma? This is my code. I got a working fiddle. But it has ... More on stackoverflow.com
🌐 stackoverflow.com
regex to remove multiple comma and spaces from string in javascript - Stack Overflow
I have a string like var str=" , this, is a ,,, test string , , to find regex,,in js. , "; in which there are multiple spaces in beginning,middle and end of string with commas. i need this strin... More on stackoverflow.com
🌐 stackoverflow.com
Removing commas in a string
Hey guys, i have a basic js question here… how can i remove commas in a string and replace them with a space. let myString = 'hello,this,is,a,difficult,to,read,sentence'; More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
September 18, 2019
javascript - Remove leading comma from a string - Stack Overflow
I have the following string: ",'first string','more','even more'" I want to transform this into an Array but obviously this is not valid due to the first comma. How can I remove the first comma fr... More on stackoverflow.com
🌐 stackoverflow.com
🌐
LinkedIn
linkedin.com › pulse › how-remove-comma-from-string-javascript-xaufc
How to remove comma from string in JavaScript
March 13, 2024 - let myString = "This, is, a, string, with, commas."; let newString = myString.split(",").join(""); console.log(newString); // Output: This is a string with commas. ... join("") joins the elements of the split array back into a string, but with ...
🌐
Java2Blog
java2blog.com › home › core java › remove comma from string in javascript
Remove Comma from String in JavaScript - Java2Blog
May 24, 2022 - Replacement String – String with which we want to replace matched string. Let’s go through regular expression /\,/g used in above replace method. The forward slashes mark start and end of regular expression · We have used \ before , to escape it as , has special meaning in regular expression. g denotes global replacement here. It is flag at end of regex which depicts that we want to remove all the commas not only first one.
🌐
Delft Stack
delftstack.com › home › howto › javascript › remove commas from string javascript
How to Remove Commas From String in JavaScript | Delft Stack
February 2, 2024 - The replace() method accepts a regular expression pattern and a replacement string. It’ll use the regular expression pattern to find the commas in the string, replacing the commas with the replacement string.
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-remove-all-commas-from-string
Remove/Replace all Commas from a String in JavaScript | bobbyhadz
The replaceAll() method will remove all commas from the string by replacing them with empty strings.
Find elsewhere
🌐
Reactgo
reactgo.com › home › how to remove commas from a string in javascript
How to remove commas from a string in JavaScript | Reactgo
August 16, 2021 - To remove the commas from a string, we can use the replace() method in JavaScript.
🌐
C# Corner
c-sharpcorner.com › blogs › javascriptremove-comma-from-the-given-users-string1
Javascript-Remove Comma from the given user's string.
April 30, 2020 - <script type="text/javascript"> function DeleteComma(MyString) { //Replace Comma By Null.... var afterreplace=MyString.replace(',',''); alert(afterreplace); } </script> </head> <body bgcolor=#00ff00 alink=blue vlink=blue> <FORM name="windowEvent"> Enter String : <input type="text" name="txtOriginal" /><br> <input type="button" value="Remove Comma" name="btnRemoveComma" onClick="DeleteComma(txtOriginal.value);" /> </FORM> </body> </html> People also reading ·
🌐
Itsourcecode
itsourcecode.com › home › how to remove commas from string javascript? 3 methods
How To Remove Commas From String JavaScript? 3 Methods - Itsourcecode.com
May 13, 2026 - Learn How To Remove Commas From String JavaScript with help of 3 methods namely, replace method, join and split method and regular expression.
🌐
JavaScript in Plain English
javascript.plainenglish.io › how-to-remove-a-leading-comma-from-a-javascript-string-e703db36d975
How to Remove a Leading Comma from a JavaScript String | JavaScript in Plain English
June 16, 2022 - We can use the JavaScript string substring method to get the part of a string. Therefore, we can use it to return a string without the leading comma. ... const myOriginalString = ",'first string','more','even more'"; const newString = ...
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Removing commas in a string - JavaScript - The freeCodeCamp Forum
September 18, 2019 - Hey guys, i have a basic js question here… how can i remove commas in a string and replace them with a space. let myString = 'hello,this,is,a,difficult,to,read,sentence';
🌐
3schools
3schools.in › home › community › how to remove last comma from string in javascript
How to Remove Last Comma from String in JavaScript - 3schools
February 25, 2023 - This article will show you how to remove the extra comma. You can use the replace() method to remove the last comma from a string. The syntax of the replace() method is given below.
🌐
Transcoding
transcoding.org › home › stripping away the commas: a javascript journey
Stripping Away the Commas: A JavaScript Journey — Transcoding
February 3, 2024 - Here, we split the string into an array by commas, then immediately join it back into a string without any separator. It’s like telling the commas, “Take a break, buddies. You’re not needed here.” · Moving on to a third-party helper, let’s talk Lodash, a utility library that’s like a multi-tool for JavaScript. It’s got a function for nearly everything, but when it comes to removing characters, we’re back to basics with _.replace(). import _ from 'lodash'; let messyString = "1,234,567"; let cleanString = _.replace(messyString, /,/g, ""); console.log(cleanString); // Outputs: 1234567
🌐
Reddit
reddit.com › r/learnjavascript › remove comma from an array
r/learnjavascript on Reddit: Remove Comma from an Array
January 21, 2022 -

I created a div element and appended an array into it. However, when the array is printed, it shows the commas as well. Is there a way to remove the commas when printing the array? (Please ignore the really weird choice of words, I just placed random words in the array to test something).

Input Output