Skip to main content

How to Join an Array into a String in JavaScript with join

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

array.join(separator) turns an array into one string, with separator between the items: ['red', 'green'].join(', ') gives 'red, green'. Without a separator it uses a comma, and an empty array gives an empty string.

Syntax​

Syntax
<array>.join(<separator>)
PartWhat it does
<array>The array whose items you want in one string.
<separator>Optional. The text that goes between items, such as ', ' or ' / '. Leave it out for a comma, or pass '' for nothing at all.

It returns a new string and leaves the array as it is. MDN's join reference: "The join() method of Array instances returns a new string that is the concatenation of all elements in this array, separated by commas or a specified separator string. If the array has only one item, that item's stringification is returned without using the separator."

The Example​

The example joins three colours with ', ', with no separator, and with an empty string. Then it joins an array with one item, checks what an empty array gives, and joins an array that holds null.

Prerequisites​

  • Node.js. This guide uses Node.js 26.13.1.

Step 1: Write the Code​

Create a file named main.js:

main.js, which joins red, green and blue with a comma and space, with no separator and with an empty string, joins an array of only red, checks that an empty array joins to an empty string, and joins red, null and blue with a slash.

main.js
const colors = ['red', 'green', 'blue'];

console.log(colors.join(', '));
console.log(colors.join());
console.log(colors.join(''));
console.log(['red'].join(', '));
console.log([].join(', ') === '');
console.log(['red', null, 'blue'].join(' / '));

Step 2: Run It​

Terminal
node main.js
Output
red, green, blue
red,green,blue
redgreenblue
red
true
red / / blue

A zsh terminal where node main.js prints red, green, blue with spaces, then red,green,blue, redgreenblue, red, true, and red / / blue with two spaces between the slashes.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • ', ' put a comma and a space between items: red, green, blue.
  • No separator meant a comma with no space: red,green,blue.
  • '' joined the items with nothing between them: redgreenblue.
  • One item came back on its own, with no separator.
  • An empty array gave an empty string, so the comparison with '' printed true.
  • null became empty text, leaving the two separators side by side: red / / blue.

Common Mistakes​

MistakeWhat happensDo this instead
Calling join on a string or an objectTypeError: roles.join is not a functionCheck Array.isArray(roles) first
Expecting join() to add a space after each commaIt uses a comma alonePass ', '
Joining an array of objectsEach object becomes [object Object]Turn each item into text first, for example with map

Troubleshooting​

SymptomLikely causeFix
Two separators appear next to each otherAn item is null, undefined or an empty stringRemove the empty items first, for example with filter
Nested arrays come out with commas insideAn inner array is joined with commasFlatten the array first with flat()

FAQ​

What's the difference between join and toString?​

For an array, toString() gives the same result as join() with no separator: the items with commas. join lets you choose the separator.

Can I join numbers?​

Yes. Each number becomes text: [1, 2, 3].join(' + ') gives '1 + 2 + 3'.

Conclusion​

join is the quickest way to show an array as text. Pick the separator yourself, and make sure the value is an array before calling it.

Additional Resources​