Array.prototype.join()
Straightforward until a null sneaks in — nullish elements become empty strings rather than the text "null", producing doubled separators that are easy to misread as a data bug.
Demo
The separator goes BETWEEN elements, never at the ends — so three elements produce two separators. A single element produces none at all, and an empty array gives an empty string rather than the separator or null. An empty separator concatenates the elements directly, which is the usual way to build a string from characters.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| separator | string | no (',') | Placed between elements. Defaults to a comma — pass an empty string to concatenate with nothing between. |
Return value
string — All elements converted to strings and concatenated with the separator between them. An empty array gives an empty string.
Common patterns
const label = tags.join(', ');
const word = chars.join('');
const path = segments.join('/');
Examples
Pitfalls
[1, null, 3].join('-')
[1, null, 3].filter(x => x != null).join('-')
['a', 'b', 'c'].join()
['a', 'b', 'c'].join('')
[1, [2, 3]].join('-')
[1, [2, 3]].flat().join('-')
[{n: 1}, {n: 2}].join(', ')
[{n: 1}, {n: 2}].map(o => o.n).join(', ')
When to use
- Turning an array into a human-readable string
- Building delimiter-separated formats like paths and CSV rows
- Concatenating characters with an empty separator
- The array may hold null or undefined → filter first
- The elements are objects → map to a field first
- You need real CSV escaping → a CSV library, not join
Notes
FAQ
Because an element was null or undefined, and join converts both to an empty string rather than to text. The separators on either side end up adjacent. Filter the array first if nullish values are possible.
[1, null, 3].join('-') // '1--3'