Explain Codes LogoExplain Codes Logo

Push multiple elements to array

javascript
spread-operator
performance-implications
best-practices
Alex KataevbyAlex Kataev·Sep 18, 2024
TLDR

Add multiple elements into an array using push with the ... (spread) operator for a quick copy-paste code snippet:

let array = [1, 2, 3]; array.push(...[4, 5, 6]); // Now array is as interesting as The Avengers, array = [1, 2, 3, "Iron Man", "Hulk", "Thor"]

Or combine two arrays into one with concat in a way that creates a new united array just like a team:

let team = [1, 2, 3].concat([4, 5, 6]); // team is now bigger, team = [1, 2, 3, "Iron Man", "Hulk", "Thor"]

Getting deeper: The Art of Array push

For extending arrays, JavaScript hands you a variety of tools. The spread operator tops the list with its simplicity and efficiency.

Large data handling

For bigger data sets, you might use:

  • Loops: Provides you full control like a proper programmer!
  • Batching with spread: A smart move to avoid stack overflow errors, which are as bad as Thanos in the Avengers universe.

Non-mutative method

In cases where the original array should not be changed, use concat as it creates a new array. It's the superpower you need to keep your data structures immutable.

Old browser compatibility

For older browser support, tools like Babel come to the rescue to support ES6 features like the spread operator.

Throwback: push.apply

Array.prototype.push.apply was the hero for adding multiple elements to an array back in ES5.

Array.prototype.push.apply(codingAvengers, aspiringAvengers);

Though it lacks the simplicity and intuitiveness of the spread and you need to provide the context parameter explicitly.

Techniques at a glance

TechniqueSyntaxUse case
Spread operatorcodingAvengers.push(...avengersAssemble);Elegant, modern, concise
concatcodingAvengers.concat(avengersAssemble);Immutable, perfect for functional programming
LoopsavengersAssemble.forEach(hero => codingAvengers.push(hero));Handles large data, avoids stack overflow
push.applyArray.prototype.push.apply(codingAvengers, avengersAssemble);Old school, but gets the job done!

Beware of pitfalls and best practices

Adopting best practices and dodging the pitfalls makes it a smooth journey.

Performance

The spread operator may lead to performance implications for massive arrays. So, keep your codebase well-balanced like a skilled tightrope walker.

Avoiding mutation

push modifies the original array, while concat creates a new one. Be aware of mutations when the Hulk is around!

The versatile spread operator

The ... spread operator can also copy arrays:

let clone = [...array]; // just like creating another Hulk!

Or spread elements into function arguments:

Math.max(...array); // who lifts the heaviest? Let's find out.

Readability

The spread syntax leads to less cognitive load and a cleaner codebase, making it easier for others to understand your code. Unreadable code can sometimes be a villain in the developer's life!