Working with arrays is one of the first skills every
JavaScript developer must master. Arrays allow you to store and manage
collections of data — from names and numbers to complex objects.
But what if you want to add multiple items to an array at
once?
This guide will walk you through all the possible ways to
do it: the classic method, modern ES6 techniques, real-world scenarios, and a
few advanced tricks. By the end, you'll not only know the syntax but also when
and why to use each method.
What You’ll Learn:
- The
basics of using push() with multiple values
- The
power of the spread operator for cleaner code
- How
to avoid nested arrays accidentally
- Real-world
examples like merging API data
- A custom function to push only unique values
- Practice problems to test your knowledge
How to push multiple elements in an array in JavaScript?
The push() method is one of the most commonly used tools in JavaScript to add elements to the end of an array. It’s simple, flexible, and allows you to insert one or more values at once.
array.push(value1, value2, ..., valueN);
example
let fruits = ['apple'];
fruits.push('banana', 'orange');
console.log(fruits);
// ['apple', 'banana', 'orange']
This works great for adding individual values. You can
pass as many arguments as you like, and JavaScript will add them one by one.
How to push array into another array in javascript?
Sometimes, you'll want to push another array into the
existing one. Here's what not to do:
let newFruits = ['grape', 'kiwi'];
fruits.push(newFruits);
console.log(fruits);
// ['apple', 'banana', 'orange', ['grape', 'kiwi']]
This creates a nested array — a new array inside your
original array. This might break your logic if you're expecting a single-level
list.
How to push elements into array using Spread Operator (...)
To flatten and add elements from another array, use the spread
operator. Introduced in ES6, it allows you to "unpack" the values of
an array.
let moreFruits = ['grape', 'kiwi'];
fruits.push(...moreFruits);
console.log(fruits);
// ['apple', 'banana', 'orange', 'grape', 'kiwi']
Why Use Spread?
- Avoids
nested arrays
- Cleaner
and more readable
- Perfect
for merging multiple arrays
Real-World Examples
You’ll Actually Use
Here’s how you’ll use this in day-to-day coding.
Example 1: Combining
API Data
let allUsers = [];
let apiPage1 = ['Alice', 'Bob'];
let apiPage2 = ['Charlie', 'Dave'];
allUsers.push(...apiPage1, ...apiPage2);
console.log(allUsers);
// ['Alice', 'Bob', 'Charlie', 'Dave']
This is common when dealing with paginated API responses.
Example 2: Dynamically
Adding Values
let numbers = [1, 2];
function addNumbers(arr) {
numbers.push(...arr);
}
addNumbers([3, 4, 5]);
console.log(numbers);
// [1, 2, 3, 4, 5]
This is great for apps where users select or add multiple
items at once.
How to Add Unique Values to an Array Without Duplicates
Sometimes you want to add new values but only if they’re
not already in the array. Here’s how:
Custom pushUnique() Function
function pushUnique(arr, ...values) {
values.forEach(val => {
if (!arr.includes(val)) {
arr.push(val);
}
});
}
Try It Out:
let tags = ['html', 'css'];
pushUnique(tags, 'css', 'javascript', 'html', 'react');
console.log(tags);
// ['html', 'css', 'javascript', 'react']
This is ideal for things like tags, categories, or
filters where duplicates can cause bugs or confusion.
Push multiple element into array using concat()
If you don’t want to modify the original array, use concat().
let fruits = ['apple', 'banana'];
let tropical = ['mango', 'papaya'];
let allFruits = fruits.concat(tropical);
console.log(allFruits);
// ['apple', 'banana', 'mango', 'papaya']
This returns a new
array, leaving the original untouched — perfect for functional programming or
React state updates.
Extra Tips & Tricks
- You can combine push() and conditionals to filter inputs.
- Use .forEach() or .map() for more customized logic.
- If working with large data, consider Set to remove duplicates efficiently:
let arr = [1, 2];
arr = [...new Set([...arr, 2, 3, 4])];
console.log(arr); // [1, 2, 3, 4]
Practice Questions
(Test Your Understanding)
Give these a try — they’ll solidify your knowledge.
🔹 Q1: What will this log?
let arr = [1, 2];
let extra = [3, 4];
arr.push(extra);
console.log(arr);
🔹 Q2: Fix this code to
avoid nesting
let names = ['Alice'];
let moreNames = ['Bob', 'Charlie'];
names.push(moreNames);
🔹 Q3: Convert the
following to use the spread operator
let cities = ['Delhi'];
let newCities = ['Mumbai', 'Pune'];
// Add newCities to cities
conclusion
Final Thoughts: When to
Use What?
Choosing between push(), push(...array), and concat()
depends on the situation:
- Use
push(a, b, c) if you’re adding individual items.
- Use
push(...array) if you’re merging arrays and want to modify the original.
- Use
concat() if you want to keep the original array unchanged.
- Use
a custom function or Set for removing duplicates.
In any case, mastering these methods will make your
JavaScript code cleaner, faster, and more reliable.