How to Join an Array into a String in JavaScript with join
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
<array>.join(<separator>)
| Part | What 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:

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
node main.js
red, green, blue
red,green,blue
redgreenblue
red
true
red / / blue

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
''printedtrue. nullbecame empty text, leaving the two separators side by side:red / / blue.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Calling join on a string or an object | TypeError: roles.join is not a function | Check Array.isArray(roles) first |
Expecting join() to add a space after each comma | It uses a comma alone | Pass ', ' |
| Joining an array of objects | Each object becomes [object Object] | Turn each item into text first, for example with map |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Two separators appear next to each other | An item is null, undefined or an empty string | Remove the empty items first, for example with filter |
| Nested arrays come out with commas inside | An inner array is joined with commas | Flatten 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
- Array.prototype.join(), on MDN.
