How to combine an array into a string in JavaScript

Published on

The Array.join() method allows you to combine an array into a single string. By default each element from the array will be separated by a comma in the returned string. Here's a very simple example of it's usage.

const names = ['Bill', 'Ben', 'Beth'];

console.log(names.join());

// Bill,Ben,Beth

It's also possible to pass the join method a parameter to override the delimiter. For example if we wanted to instead have each element separated by an empty space we could do the following.

const names = ['Bill', 'Ben', 'Beth'];

console.log(names.join(' '));

// Bill Ben Beth

If there are no elements in the array join will return an empty string.

const names = [];

const joined = names.join();

// joined = ''