Removing commas from javascript array

javascript array to string without commas
remove comma from string javascript
array join javascript
print array without commas javascript
remove first and last comma from string in javascript
regex remove commas
convert comma separated string to array javascript
javascript split string by comma

I have 3 strings "a","b","c" in javascript array "testarray".

var testarray=new Array("a","b","c");

and then I am printing the value of testarray using javascript alert box.


The result will be like a,b,c

All these strings are separated by "," character. I want to replace this "," with some other character or combination of two or more characters so that the alert box will show something like a%b%c or a%$b%$c

Use the join method:

alert(testarray.join("%")); // 'a%b%c'

Here's a working example. Note that by passing the empty string to join you can get the concatenation of all elements of the array:

alert(testarray.join("")); // 'abc'

Side note: it's generally considered better practice to use an array literal instead of the Array constructor when creating an array:

var testarray = ["a", "b", "c"];

Use join Method. The join() method joins all elements of an array (or an array-like object) into a string and returns this string. The elements will be separated by a specified separator. The default separator is comma (,). 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.

use testarray is getting converted in string using testarray.toString() before alert. toString internally joining these items using ',' as separator. you can convert it into string using Array.join and pass own separator.


Learn the many ways to use JS to remove one or more commas from a string. At this point you could loop over the array and do something if you wanted,  You can use arr.join(glue) to concatenate Array elements with something inbetween. In your case glue would be an empty string arr.join("") . share | improve this answer

you can iterate through the array and insert your characters

var testarray=new Array("a","b","c");
var str;
for (var i = 0; i < testarray.length; i++) {

While the extra comma shouldn't harm anything (JS does not care in all modern browsers), you'd better off encoding this in JSON beforehand: It will not work suppose Array selIdss has values like 1,2,3 but replace will only remove first comma output will be 1;2,3 – panky sharma Apr 22 '15 at 7:13

The .join() method is just to merge or concatenate elements in an array using a JavaScript Glossary - Lesson 14 of 29 in an array and returns it as a string separated by a comma ( , ) or any specified Remove all commas within a string. How do I remove commas while displaying an array in JavaScript? Use join Method The join() method joins all elements of an array (or an array-like object) into a string and returns this string. The elements will be separated by a specified separator. The default separator is comma (,).

Here are some more FAQ related to this topic: How to check if a value exists in an array in JavaScript · How to remove duplicate values from a JavaScript array  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.

How to remove options from selectlist in javascript? how to remove session data on tab close in redux\ · how to remove spaces from strings  I have a one-dimensional array of strings in JavaScript that I'd like to turn into a comma-separated list. Is there a simple way in garden-variety JavaScript (or jQuery) to turn that into a comma-

  • I am being over picky here but declare arrays using var testarray= ["a","b","c"] rather than var testarray=new Array("a","b","c");it reads easier an avoids a few potential issues.
  • thanks for the tip. will figure it out next time :)