JavaScript

How to Remove Commas From String in JavaScript

A comma is a punctuation mark used in long sentences to make them more readable. It can be difficult for a developer to delete all the commas from a collection of strings manually. To do so, JavaScript has certain predefined methods that help the developers remove commas from a text.

This post will describe the method for eliminating commas from a string in JavaScript.

How to Remove Commas From String in JavaScript?

To eliminate commas from a string in JavaScript, use the following JavaScript predefined methods:

    • replace() method
    • replaceAll() method
    • Combination of split() method with join() method

Let’s discuss all of the above-mentioned methods one by one.

Method 1: Remove Commas From String Using replace() Method

The “replace()” method simply replaces a string’s value with the defined string. It requires two arguments, the value that will be replaced and the value to be utilized as a replacer. By default, it just removes the first instance of the searched value. However, with the help of regular expression, it can be made to remove all the occurrences of the searched term.

Syntax

Follow the below-mentioned syntax to use the replace() method for removing commas from the string:

replace("replaceValue","replacer");

 
Here, “replaceValue” is the searched value that will be replaced in a string, and the“replacer” is used to replace it. It gives a new string with the replaced values as output.

Example

First, create a variable “str” and store a string “Linuxhint, is the best, website for learning, skills” in str:

var str = 'Linuxhint, is the best, website for learning, skills';

 
Call the replace() method by passing comma (,) and an empty string (‘’) as an argument in “console.log()” method:

console.log(str.replace(',', ''));

 
The given output removes just the first comma from a string:


To remove all the commas from the string using the replace() method, pass the regex pattern “/\,/g” as a replaceValue instead of a comma (,):

console.log(str.replace(/\,/g, ''));

 
Here, in regular expression, the forward slashes (/) indicates the start and end of a regular expression, while the backslash (\) is used with comma (,) as an escape character, “g” is the global flag that represents to eliminate all commas from a string.

The output shows that all the commas from the string are removed:

Method 2: Remove Commas From String Using replaceAll() Method

The “replaceAll()” method is another JavaScript built-in method. It also requires two parameters, the value to be replaced and the value to be used as a replacer. It is specifically utilized when it is necessary to replace all of the stated values at once without using a regex pattern.

Syntax

Follow the below-provided syntax for the replaceAll() method to eliminate commas from a string:

replaceAll("searchValue", "replaceValue");

 
In the above syntax, the “searchValue” is the substring to replace, and “replaceValue” is the value that is used as a replacer. When a specific value is found in the string, it outputs a new string with the replaced values.

Example

Call the replaceAll() method by passing the comma (,) in the first argument and an empty string (‘’) in the second argument. The replaceAll() method will replace all the commas from a string with an empty substring:

console.log(str.replaceAll(',', ''));

 
In the output, all the commas have now been removed:

Method 3: Remove Commas From String Using split() Method With join() Method

The “split()” with the “join()” method, is another procedure in JavaScript used for removing commas from the string. The split() method returns an array. This array contains components that represent parts of the strings when the split encounters a comma. However, we require a string rather than an array. For that, use the join() method with the split() method to convert the array to a string.

Syntax

The below-mentioned syntax is used for the split() method with the join() method:

split(",").join("")

 
The split() method will accept the comma as a parameter and return an array of substrings. The arrays are rejoined into a string using JavaScript join() method by passing an empty string.

Example

Invoke the split() method with join() method in “console.log()” method by passing comma(,) and empty string(‘’) as an arguments:

console.log(str.split(',').join(''));

 
Output

Conclusion

To remove commas from a string value, use either the replace() method, the replaceAll() method or the combination of split() and join() method. The replace() only removes the first comma from the string due to its default working. However, with the help of a regular expression, it can be customized to remove all the commas from the given string. The replaceAll() removes all the occurrences of a comma “,” from the entire string. The duo of split() and join() also perform the same operation; however, they work differently. All of these have been thoroughly explained in this post with examples.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.