Convert an array to a string in JavaScript with join()

items.join(separator) returns one string with the separator between array elements. Omitting the separator or passing undefined uses a comma. Pass '' to join without any separator. The original array stays the same.

Choose a separator to build a string

Run the snippet in the console. join(', ') includes a space after the comma. The separator appears only between elements, so three elements produce two separators. An empty array produces an empty string. With join(null), the separator is the string 'null'. See ECMAScript: Array.prototype.join().

const places = ['Seoul', 'Busan', 'Jeju'];
console.log(places.join());      // 'Seoul,Busan,Jeju'
console.log(places.join(', '));  // 'Seoul, Busan, Jeju'
console.log(places.join(' / ')); // 'Seoul / Busan / Jeju'
console.log(places.join(''));    // 'SeoulBusanJeju'
console.log([].join('-'));       // ''
console.log(places);            // ['Seoul', 'Busan', 'Jeju']
console.log(['A', 'B'].join(null)); // 'AnullB'

Handle null, undefined, and holes

An element containing null or undefined, or a hole in an ordinary array, contributes an empty part. The position still counts, so adjacent separators can appear, as in A||C.

console.log(['A', null, 'C'].join('|'));      // 'A||C'
console.log(['A', undefined, 'C'].join('|')); // 'A||C'
console.log(['A', , 'C'].join('|'));         // 'A||C'
console.log(['A', null, 'C'].join(''));     // 'AC'

To omit empty parts deliberately, filter the array first. The example excludes null, undefined, and empty strings while keeping 0 and false. filter(Boolean) would discard those two values as well.

const values = ['A', null, 0, undefined, false, '', 'C'];
const kept = values.filter(value =>
  value !== null && value !== undefined && value !== ''
);
console.log(kept.join(' | ')); // 'A | 0 | false | C'

Complete example: change the array and separator

Save the HTML in UTF-8 as array-join.html and open it in a browser. Change the default comma to a slash, then choose Custom and type | . Spaces you enter on either side of a separator are included in the result.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Practice with join() separators</title>
</head>
<body>
<label for="sample">Array</label>
<select id="sample">
  <option value="places">Seoul, Busan, Jeju</option>
  <option value="missing">Contains null and undefined</option>
</select>
<label for="separator">Separator</label>
<select id="separator">
  <option value="default">Default comma</option>
  <option value="slash">Slash with surrounding spaces</option>
  <option value="empty">Empty string</option>
  <option value="custom">Custom</option>
</select>
<label for="custom">Custom separator</label>
<input id="custom" type="text" value=" | " disabled>
<pre id="output" aria-live="polite"></pre>
<script>
const samples = {
  places: ['Seoul', 'Busan', 'Jeju'],
  missing: ['Seoul', null, 'Busan', undefined, 'Jeju'],
};
const sample = document.querySelector('#sample');
const separator = document.querySelector('#separator');
const custom = document.querySelector('#custom');
const output = document.querySelector('#output');

function render() {
  const items = samples[sample.value];
  let result;
  if (separator.value === 'default') result = items.join();
  else if (separator.value === 'empty') result = items.join('');
  else if (separator.value === 'custom') result = items.join(custom.value);
  else result = items.join(' / ');
  custom.disabled = separator.value !== 'custom';
  const source = items.map(item => item === undefined ? 'undefined' : JSON.stringify(item));
  output.textContent = `Result string: ${JSON.stringify(result)}\n` +
    `Source: [${source.join(', ')}]\n` +
    `Result type: ${typeof result}`;
}

sample.addEventListener('change', render);
separator.addEventListener('change', render);
custom.addEventListener('input', render);
render();
</script>
</body>
</html>

Choose the sample containing null and undefined and switch back to the default comma to get "Seoul,,Busan,,Jeju". Compare the distinct values in the Source line with the consecutive commas in the result. The last line confirms the result is a string.

When split() can reconstruct a joined array

Splitting with the same separator can recover the elements of a nonempty string array when the separator is a single character such as a comma and does not occur inside any element. If an element already contains the separator, the joined text cannot tell you where the original element boundaries were. The example joins two names with a comma and space, then split(', ') breaks them into four pieces. A nested array also loses structure. For example, ['A', ['B', 'C']].join(' / ') gives 'A / B,C'.

const names = ['Kim, Mina', 'Lee, Jun'];
const text = names.join(', ');
console.log(text);             // 'Kim, Mina, Lee, Jun'
console.log(text.split(', ')); // ['Kim', 'Mina', 'Lee', 'Jun']

const serialized = JSON.stringify(names);
console.log(JSON.parse(serialized)); // ['Kim, Mina', 'Lee, Jun']

Use JSON.stringify() and JSON.parse() to preserve the boundaries of a string array. The example restores both names intact. JSON encodes array elements holding undefined and holes as null, so use a separate representation if you need those distinctions. See ECMAScript: JSON.stringify().