Append to a JavaScript array: push, spread, concat (and prepend with unshift)

How to append to arrays in JavaScript: javascript append to array, js append to array with push and return value, push(...arr), spread literals, concat for new arrays, nested push gotcha, unshift prepend, and push.apply without spread. Snippets include short expected-output notes from Node runs.

Published

Updated

Read time 4 min read

Reviewed byDeepak Prasad

Append to a JavaScript array: push, spread, concat (and prepend with unshift)

Javascript append to array at the end is usually done with Array.prototype.push, optionally with the spread operator to js append to array values from another array in one call. For a new array without touching the old reference, use concat or [...existing, ...more]. This guide matches how to add to an array in JavaScript (broader insert/splice coverage) but focuses on end append patterns. For front insertion, see unshift below and JavaScript unshift(); for ... syntax in general, see the spread operator in JavaScript.

Tested on: Node.js v20.18.2. A short note after each snippet describes what you should see in the console.


Quick reference

End-of-array work is either mutate (push, unshift) or allocate (concat, spread)—the right row depends on whether other code still relies on the original array reference.

Situation Prefer
Mutate existing array at end push / push(...items)
Keep old array reference valid concat or [...a, ...b] into a new variable
Prepend unshift / unshift(...items)
Oldest engines without spread push.apply (see caveats in FAQ)

push — append one or more values (mutates)

push appends elements and returns the new length, not the array reference.

javascript
let fruits = ["apple", "banana", "orange"];
fruits.push("tangerine");
console.log(fruits);
Output

You should see one line like: [ 'apple', 'banana', 'orange', 'tangerine' ].

push returns the new length, not the array:

javascript
const arr = ["a", "b"];
const n = arr.push("c");
console.log(n);
console.log(arr);
Output

You should see 2 lines: 3, then [ 'a', 'b', 'c' ].


push(...otherArray) — append array to array in place

Spread the source so each element is appended; a bare array argument becomes a single nested element.

javascript
let numbers = [13, 72, 93];
const newNumbers = [88, 45, 70];
numbers.push(...newNumbers);
console.log(numbers);
Output

You should see one line like: [ 13, 72, 93, 88, 45, 70 ].

Without ..., the second array becomes one nested element:

javascript
const a = [1, 2];
a.push([3, 4]);
console.log(a);
Output

You should see one line like: [ 1, 2, [ 3, 4 ] ].


Spread literal — append to array javascript immutably

Build a new array so callers still holding the old reference see the previous contents.

javascript
let fruits = ["apple", "banana", "orange"];
fruits = [...fruits, "tangerine"];
console.log(fruits);
Output

You should see one line like: [ 'apple', 'banana', 'orange', 'tangerine' ].

Merge two arrays into a new value:

javascript
let numbers = [1, 2, 3];
const newNumbers = [4, 5, 6];
numbers = [...numbers, ...newNumbers];
console.log(numbers);
Output

You should see one line like: [ 1, 2, 3, 4, 5, 6 ].


concat — new array, originals unchanged

concat does not change fruits or newFruits; only the returned array combines them.

javascript
const fruits = ["apple", "banana", "orange"];
const newFruits = ["tangerine", "watermelon"];
const allFruits = fruits.concat(newFruits);
console.log(allFruits);
Output

You should see one line like: [ 'apple', 'banana', 'orange', 'tangerine', 'watermelon' ].


unshift — prepend at the start (not the end)

Strictly this is not “append,” but the same ... pattern is often paired with push in tutorials. unshift mutates and returns the new length.

javascript
let fruits = ["apple", "banana", "orange"];
fruits.unshift("tangerine");
console.log(fruits);
Output

You should see one line like: [ 'tangerine', 'apple', 'banana', 'orange' ].

Prepend all elements of another array:

javascript
let numbers = [13, 72, 93];
const newNumbers = [88, 45, 70];
numbers.unshift(...newNumbers);
console.log(numbers);
Output

You should see one line like: [ 88, 45, 70, 13, 72, 93 ].


Optional: push.apply (no spread)

javascript
const arr = [0, 1, 2];
const arr2 = [3, 4, 5];
Array.prototype.push.apply(arr, arr2);
console.log(arr);
Output

You should see one line like: [ 0, 1, 2, 3, 4, 5 ].


Summary

  • push / push(...items) mutates at the end and returns the new numeric length.
  • Spread or concat builds a new array when you must not disturb existing references.
  • unshift prepends; push.apply is the classic pre-spread escape hatch.
  • Remember push(otherArray) nests the array unless you spread it.

References

MDN references for mutating and immutable array assembly.


Frequently Asked Questions

1. How do I javascript append to array at the end?

Use push: arr.push(item) mutates the array and returns the new length. For several values at once, pass multiple arguments: arr.push(a, b, c).

2. How do I js append to array from another array (append array to array)?

Mutate the first array in place with first.push(...second). For a new array without changing the originals, use first.concat(second) or const out = [...first, ...second].

3. What does push return—the new array?

No. push returns the new numeric length after the insert, not a reference to the array.

4. I used push(myOtherArray) and got a nested array—why?

push adds its arguments as elements. A single array argument becomes one element. Use push(...myOtherArray) to append each element, or concat/spread if you prefer a new array.

5. append to array javascript without mutating the original?

Use concat or a spread literal assigned to a new variable: const next = [...arr, item] or const next = arr.concat(item). That leaves arr unchanged.

6. No spread operator—how did we append arrays in older JavaScript?

Array.prototype.push.apply(target, source) appends each element of source to target. Prefer spread on modern engines; for extremely large sources, a loop or concat avoids argument-list limits.
Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs …