Merge JavaScript arrays with concat() and keep the originals

first.concat(second) returns a new array with the elements of second after those of first. For ordinary arrays, the originals retain their length and order. Passing a string or a number adds one value as one element.

Concatenate values and arrays

Run the snippet in the console. Passing two strings and passing an array containing those strings yield the same order. The last log shows that first still contains just two elements.

const first = ['Lorem', 'Ipsum'];
const second = ['Dolor', 'Amet'];

console.log(first.concat('Dolor', 'Amet'));
// ['Lorem', 'Ipsum', 'Dolor', 'Amet']
console.log(first.concat(second));
// ['Lorem', 'Ipsum', 'Dolor', 'Amet']
console.log(first); // ['Lorem', 'Ipsum']

Save the returned array with const combined = first.concat(second) if you need to use it later. first.concat() with no arguments makes a shallow copy. See ECMAScript: Array.prototype.concat() for the rules, including how Symbol.isConcatSpreadable can change the default.

Add an array as a single element

An ordinary array argument contributes only its outer elements. Wrap it with first.concat([second]) to add the whole array as one element. The first result has three strings, while the second ends with an array. Use flat() with the desired depth to flatten an already nested array.

const first = ['A'];
const second = ['B', 'C'];
console.log(first.concat(second));   // ['A', 'B', 'C']
console.log(first.concat([second])); // ['A', ['B', 'C']]
console.log(first.concat([['D']]));  // ['A', ['D']]

Complete example: compare three argument shapes

Save the HTML in UTF-8 as array-concat-example.html and open it in a browser. With the default input Dolor, Amet, select each mode and click Show result. JSON.stringify() displays nested brackets so you can compare the results.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Compare concat arguments</title>
</head>
<body>
<label for="values">Values to add (comma-separated)</label>
<input id="values" type="text" value="Dolor, Amet">
<label for="mode">Concatenation mode</label>
<select id="mode">
  <option value="array">Append the elements of the array</option>
  <option value="single">Append the input as one string</option>
  <option value="nested">Append the array as one element</option>
</select>
<button id="run" type="button">Show result</button>
<button id="reset" type="button">Reset</button>
<pre id="output" aria-live="polite"></pre>
<script>
const first = ['Lorem', 'Ipsum'];
const input = document.querySelector('#values');
const mode = document.querySelector('#mode');
const output = document.querySelector('#output');

function show() {
  const value = input.value.trim();
  if (value === '') {
    output.textContent = "Enter a value to add.";
    return;
  }
  const parts = value.split(',').map(part => part.trim());
  let joined;
  if (mode.value === 'single') {
    joined = first.concat(value);
  } else if (mode.value === 'array') {
    joined = first.concat(parts);
  } else {
    joined = first.concat([parts]);
  }
  output.textContent = `Original: ${JSON.stringify(first)}\n` +
    `Result: ${JSON.stringify(joined)}`;
}

document.querySelector('#run').addEventListener('click', show);
document.querySelector('#reset').addEventListener('click', () => {
  input.value = 'Dolor, Amet';
  mode.value = 'array';
  show();
});
show();
</script>
</body>
</html>

The default mode yields four elements in ["Lorem","Ipsum","Dolor","Amet"]. In string mode, "Dolor, Amet" is one element. In nested mode, ["Dolor","Amet"] is one element. The original retains two elements in every mode. Reset restores the input and mode.

Check shallow copies and shared references

The result has separate array positions but shares references to existing objects. Changing result[0].name changes the name visible through the original. Assigning a different object to result[0] changes only the result’s first position. The last logs show the original name after the property change and the original length.

const source = [{ name: "Mina" }];
const result = source.concat({ name: "Hana" });
console.log(result === source);    // false
console.log(result[0] === source[0]); // true
result[0].name = "Yuna";
result[0] = { name: "Jisoo" };
console.log(source[0].name); // "Yuna"
console.log(source.length);  // 1

If you need independent objects, also copy the objects you intend to change. When their properties are all strings, as here, source.map(person => ({ ...person })) makes new objects. For nested objects, decide which deeper properties need independent copies.

Choose between concat(), spread, and push()

To join two ordinary arrays without holes, you can also use [...first, ...second]. In an array literal, spread inserts values produced by an iterable. With the default array iterator, a hole becomes an element holding undefined. concat() preserves the hole in the example below. Compare the results with Object.hasOwn(). See ECMAScript: Array Initializer.

const sparse = ['A', , 'C'];
const joined = sparse.concat(['D']);
const spread = [...sparse, 'D'];

console.log(joined.length, spread.length); // 4 4
console.log(Object.hasOwn(joined, 1));     // false
console.log(Object.hasOwn(spread, 1));     // true
console.log(spread[1]);                    // undefined

Use concat() or spread for a new array. Use push() to append to the original. push() modifies that array and returns its new length. For a small second array, first.push(...second) passes its elements individually.