Skip to content
Advertisement

Easy way to turn JavaScript array into comma-separated list?

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-separated list? (I know how to iterate through the array and build the string myself by concatenation if that’s the only way.)

Advertisement

Answer

The Array.prototype.join() method:

var arr = ["Zero", "One", "Two"];

document.write(arr.join(", "));
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement