Fraser Hamilton

How to combine an array into a string in JavaScript

November 08, 2021

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']

// 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 = ''

Written by Fraser Hamilton a full stack developer based out of Edinburgh, Scotland.

© 2021, Fraser Hamilton