JavaScript add to array: push, unshift, splice, concat, spread, length

How to add to arrays in JavaScript: javascript add to array, js add to array with push, unshift, splice, toSpliced, concat, spread, length holes, javascript add array to array, mutating vs new array, and links to append and unshift guides. Snippets include short expected-output notes from Node runs.

Published

Updated

Read time 4 min read

Reviewed byDeepak Prasad

JavaScript add to array: push, unshift, splice, concat, spread, length

Javascript add to array and js add to array almost always mean either mutating the same array (push, unshift, splice, length) or building a new array (concat, spread, toSpliced). For javascript add array to array, common patterns are first.push(...second), first.concat(second), or [...first, ...second]. Pick based on whether callers must keep the old reference unchanged—see how to append to an array in JavaScript and JavaScript unshift() for deeper method guides, and the spread operator in JavaScript for ... syntax.

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


Quick reference

Most “add to array” tasks are either mutate in place (push, unshift, splice, length) or return a new array (concat, spread, toSpliced)—pick based on whether other owners still hold the old reference.

Goal Typical API Mutates original?
Add at end push, push(...items) Yes
Add at start unshift Yes
Insert / delete at index splice Yes
Insert / delete (immutable) toSpliced (ES2023) No (returns new array)
Append values or arrays concat, spread No (new array)
Grow by index length = n Yes (adds empty slots)

1. push — javascript add element to array at the end

push appends one or more elements and returns the new length—ideal for stacks and growing buffers when sharing the same array reference is intentional.

javascript
const arr = [11, 22, 33];
arr.push(34);
console.log(arr);
Output

You should see one line like: [ 11, 22, 33, 34 ].

To javascript add array to array into the same array, spread the second array so each element is appended (not nested as a single element):

javascript
const vegetables = ["parsnip", "potato"];
const moreVegs = ["celery", "beetroot"];
vegetables.push(...moreVegs);
console.log(vegetables);
Output

You should see one line like: [ 'parsnip', 'potato', 'celery', 'beetroot' ].


2. unshift — js array add at the beginning

unshift shifts existing indices upward; like push, it mutates and returns the new length.

javascript
const arr = [11, 22, 33];
arr.unshift(10);
console.log(arr);
Output

You should see one line like: [ 10, 11, 22, 33 ].


3. splice — insert at an index (mutating)

splice(start, deleteCount, ...items) can remove or insert at any position; see JavaScript splice for delete/replace patterns.

javascript
const arr = [1, 2, 3];
arr.splice(1, 0, 4);
console.log(arr);
Output

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


4. toSpliced — same idea as splice, new array (ES2023)

Useful when you want add to array javascript semantics without mutating the source (similar spirit to toSorted / toReversed).

javascript
const arr = [1, 2, 3];
const next = arr.toSpliced(1, 0, 4);
console.log(arr);
console.log(next);
Output

You should see 2 lines: [ 1, 2, 3 ], then [ 1, 4, 2, 3 ].


5. concat — new array, originals unchanged

javascript
const arrOne = ["wow", 31, true, "GoLinux"];
const arrTwo = arrOne.concat("JavaScript");
console.log(arrOne);
console.log(arrTwo);
Output

You should see 2 lines: [ 'wow', 31, true, 'GoLinux' ], then [ 'wow', 31, true, 'GoLinux', 'JavaScript' ].

Javascript add array to array with concat (one level of array elements merged):

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

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

If you need a single nested array as one element, wrap it: a.concat([b])[ 1, 2, [ 3, 4 ] ] (not shown; easy to confuse with concat(b)).


6. Spread + slice — immutable insert in the middle

javascript
const arr = [11, 22, 33];
const newArr = [...arr.slice(0, 2), 44, ...arr.slice(2)];
console.log(arr);
console.log(newArr);
Output

You should see 2 lines: [ 11, 22, 33 ], then [ 11, 22, 44, 33 ].

Equivalent merge without mutating either input:

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

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


7. length — grow (empty slots) or shrink

Growing length adds empty slots; Node prints them as <n empty items> until you assign.

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

You should see 3 lines: 4, then [ 1, 2, 3, 4, <2 empty items> ], then [ 1, 2, 3, 4, 5, 6 ].

Shrinking length truncates from the end (like repeated pop); see JavaScript Array.prototype.pop for removing the last element explicitly.

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

You should see 2 lines: 4, then [ 1, 2 ].


Summary

Mutators (push, unshift, splice, length) change the same array reference; copiers (concat, spread, toSpliced) build new arrays for immutable-style pipelines.

  • End: push or push(...items); start: unshift.
  • Middle, mutate: splice; middle, copy: toSpliced or slice + spread.
  • New top-level array: concat, spread, or toSpliced.
  • length grows or shrinks in place; new slots are holes until assigned.

References

MDN references for the main mutators and immutable variants used in javascript add to array flows.


Frequently Asked Questions

1. What is the simplest way to javascript add to array at the end?

Use push: arr.push(x) or arr.push(a, b). It mutates the array and returns the new length. For many values from another array, use arr.push(...otherArray) to append each element.

2. How do I javascript add array to array without changing the originals?

Use spread: const out = [...a, ...b]; or a.concat(b). Both give a new top-level array. concat flattens array arguments by one level; nested arrays stay nested unless you flatten on purpose.

3. add to array javascript at the start?

unshift inserts at index 0 and returns the new length. Like push, it mutates the existing array.

4. add element to array javascript in the middle?

Mutating: splice(index, 0, newItem). Non-mutating (modern engines): toSpliced(index, 0, newItem). Or build a new array with slice and spread.

5. concat vs spread for javascript array add?

concat takes multiple arguments and is fine for values and arrays. Spread is often clearer for assembling one new array literal. Both can build a new array; push mutates in place.

6. What happens if I set array.length larger than before?

The array grows; new indices are empty slots until you assign them. In Node, console.log may show rather than the word undefined for those holes.
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 …