How to Remove Commas from Array in JavaScript

← PrevNext →

You can use the join() method in JavaScript to remove commas from an array. The comma delimiter in an array works as the separator. For example, let arr = ['alpha', 'bravo', 'charlie']. Using the join() method, you can remove or replace all the commas with a space.

Remove Commas from Array using JavaScript

join() Method Syntax

arr.join(separator)

The method takes a parameter in form of a string (as separator). It can be a hyphen, pipe (|), any special character or simply an empty space. You have to specify a string value, which will be used to separate the array values from each other.

The join(), when used with an array, returns a new string concatenated using a specified separator. If there is no separator specified, it will return values with commas.

👉 Remove Empty Slots in an Array using a simple One-Line-Code in JavaScript.
Remove Empty Array Slots in JavaScript

The parameter (or the argument) is optional.

If there is only one value in the array, it will display just that one value without any separator.

It is one of the simplest ways to remove commas from an array in JavaScript.

Now, let’s see how to remove the commas using the array.join() method.

<script>
let removeComma= () => {
    let arr = ['alpha', 'bravo', 'charlie'];
    document.write (arr.join(' '));
}

removeComma();
</script>
Try it
In the above example, the parameter to the join() is a space. Therefore, it will display the array values separated by a space, removing all the commas.

Now, here’s another example, where I am using just an empty string. The commas will be replaced with no space at all.

<script>
let removeComma = () => {
    let arr = ['51', '- Lane', 'Worli'];
    document.write (arr.join(''));
}

removeComma();
</script>
Try it

👉 Arrays in JavaScript can have texts and number. Now, how to filter our only numbers in an Array. Check our this post.
Remove Empty Array Slots in JavaScript

That's it. Thanks for reading.

← PreviousNext →


Like this Article? Subscribe now, and get all the latest articles and tips, right in your inbox.
Delivered by FeedBurner