Skip to main content Accessibility Feedback

Converting an array to a string with vanilla JavaScript

You can use the Array.join() method to convert an array of items into a string.

By default, it will separate each item with a comma, but you can pass in any assortment of characters you’d like—dashes, spaces, or even an empty string to smush everything together into one long word.

var shoppingList = [
    'apples',
    'pears',
    'cookies',
    'bread'
];

// Returns "apples,pears,cookies,bread"
var shoppingListString = shoppingList.join();

// Returns "apples pears cookies bread"
var shoppingListWithSpaces = shoppingList.join(' ');

// Returns "apples - pears - cookies - bread"
var shoppingListWithDashes = shoppingList.join(' - ');

// Returns "applespearscookiesbread"
var shoppingListSmushed = shoppingList.join('');

This works in all modern browsers, and back to at least IE6.


🚀 Make 2018 the year you master JavaScript! My pocket guides and mini courses are short, focused, and made for beginners. You can do this!

Have any questions or comments about this post? Email me at chris@gomakethings.com or contact me on Twitter at @ChrisFerdinandi.

Get Daily Developer Tips