Use JavaScript to Remove Comma From String

There are a number of string manipulation tasks you can do with JavaScript, and one of them is to remove a comma from a string. You can even remove all of them if that is what you want.

You might also be interested in our guide on best computer science laptops.

TL;DR: (There are more options later in the post)

var purpose = "I, Just, want, a, quick, answer!";

// Removing the first occurrence of the comma
var result = purpose.replace(",", "");

// Removing all the commas
var result = purpose.replace(/,/g, "");

There are various ways to remove one or more commas from a string using JavaScript. We will look at 3 different ways. Two will use built in functions, and at the end there will be a way to to it manually.

1. Using the .replace() function to remove commas

We will go into more details of the “.replace()” function and also look at some other ways to remove commas from a string as well. You pass two parameters to the replace() function. The first is the value that you want to replace, and the second is what you want to replace it with. You can also pass a regular expression as the first parameter for more advanced functionality. Also, you can pass a function as the second parameter.

For example.

Removing a single comma using JavaScript

var iAmAString = "Once upon a time, long long ago"; 

var iAmAlsoAString = iAmAString.replace(",", ""); 

console.log(iAmAlsoAString);

This code will take the comma and remove it. But that was just with one comma, what if we have several?

var iHaveManyCommans = "Once, upon, a, time, long, long, ago";

var howManyCommasDoIHave = iHaveManyCommans.replace(",", "");

console.log(howManyCommasDoIHave );

You can see the output from the above code in the image below.

Using remove() on a string with many commas

As you can see, using .replace(“,”, ”) only removes the first comma from the string. If you want to use JavaScript to remove all the commas from a string, then there is a simple regular expression flag that you can add to make it accomplish that.

Removing all the commas

var iHaveManyCommans = "Once, upon, a, time, long, long, ago";

var howManyCommasDoIHave = iHaveManyCommans.replace(/,/g, '');

console.log(howManyCommasDoIHave );

Adding the g, “global”, will make it remove all the commas in your string. Going into regular expression is beyond the scope of this article, but you can use regular expression with the .replace() function for powerful string manipulation.

2. Using .split() and .join() to remove commas

There are a few other ways that we can remove commas from a string using JavaScript.

One other way is to use the .split() and .join() functions.

var iHaveManyCommans = "Once, upon, a, time, long, long, ago";

var howManyCommasDoIHave = iHaveManyCommans.split(',').join("");

console.log(howManyCommasDoIHave );

This technique will first split the string on the commas. This will return an array of words. At this point you could loop over the array and do something if you wanted, or you can use the .join() function to join all the indices on whatever you give as input. If you wanted to have a dash in-between all the words you could use .join(“-“). If you give an empty string as we do above it will put it back together just without the commas. Since there was already a space in between the words it will still have the spaces.

3. Removing commas without a built in function

You can also remove commas by doing everything manually. By manually I mean to loop over all the characters of a string and removing the comma. Since a string is immutable in JavasScript we are actually creating a new string instead of removing them from the old value. You might also want to check out using break and continue statements in JavaScript.

function removeSymbol(symbol, str){
    var newString = "";
    for(var i = 0; i < str.length; i++) {
        var char = str.charAt(i);
        if(char != symbol){
            newString = newString + char;
        }
    }
    return newString;
}

In the solution above we iterate over each character in the string. If the character is not equal to the symbol we want to remove, then we add that symbol to the new string. At the very end we return the string without any of the symbols.

When might you use any of these techniques?

A common reason for using JavaScript to remove a comma from a string is if you have a number with a thousand separator, and you want to get rid of either one or many of them. Most times you will be given a number but it is in the form of a string, but it has the comma in it so you cant use it because it is seen as a string, it is difficult to convert it too because of the commas. Using the .replace() function is a good solution to this problem.

An example is:

Lets say you have an input box where you display the number with a thousand separator. The user inputs a number and you get the value. You now have a string that is lets say “1,200”. If you now want to do math with this value you first have to remove the comma from the string. There are a number of ways you can do this, and there are three listed in this post. Being able to remove a comma with JavaScript is quite useful.

For more JavaScript knowledge you can have a look at how to check if an object contains a key here.