How to Convert an Array to a String with Commas in JavaScript
You can use a built-in function on the Array prototype to join together an array as a string separated by commas in JavaScript.
Join Together Array Items as a String in JavaScript
One of JavaScript’s most helpful built-in array methods is .join() (Array.prototype.join()
), which returns a string primitive.
“The
join()
method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string.” — MDN Docs
You can use any separator you want. In fact, the separator string can be anything at all, of any number of characters.
That means it’s easy to turn your JavaScript array into a comma-separated list format, whether you’d like to add just a comma or a comma and a space.
String with Just Commas
When you want just the comma character (,
) to appear as the separator between items in the list, use .join(",")
to convert the array to a string.