Skip to main content

push() vs concat()

The push() and concat() methods are indeed similar - both add elements to an array, but they do it in completely different ways.

1. push() - changes (mutates) the original array

javascript
const arr = [1, 2]; arr.push(3); console.log(arr); // [1, 2, 3]
  • Adds element(s) to the end of the original array.
  • Changes (mutates) the original array.
  • Returns the new length of the array, not the array itself.
javascript
const len = arr.push(4, 5); console.log(len); // 5 console.log(arr); // [1, 2, 3, 4, 5]

2. concat() - creates a new array (does not mutate)

javascript
const arr = [1, 2]; const newArr = arr.concat(3); console.log(arr); // [1, 2] - the original did not change console.log(newArr); // [1, 2, 3]
  • Does not change the original array.

  • Returns a new array.

  • You can merge several arrays or individual values:

    javascript
    const a = [1, 2]; const b = [3, 4]; const c = a.concat(b, 5, [6, 7]); console.log(c); // [1, 2, 3, 4, 5, 6, 7]

Behavior comparison

Propertypush()concat()
Changes the original arrayYesNo
ReturnsNew lengthNew array
AcceptsOne or several elementsArrays and/or elements
Creates a copyNoYes
MutationYesNo
Typical taskAdd an element to an existing arrayMerge arrays

Example of the difference:

javascript
const arr = [1, 2]; const arr2 = [3, 4]; // push() arr.push(...arr2); console.log(arr); // [1, 2, 3, 4] (the array changed) // concat() const combined = arr.concat(arr2); console.log(combined); // [1, 2, 3, 4] console.log(arr); // [1, 2] (the original was left untouched)

In short:

Characteristicpush()concat()
Mutates the arrayYesNo
Returnsa number (length)a new array
Adds at the endYesYes
Can add another arrayYes (via ...spread)Yes, directly
Use whenchanging the current arraycreating a new one

Conclusion:

  • If you need to change the current array, use push().
  • If you need to create a new one without touching the old one, use concat().

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.