How to push multiple elements in an array of JavaScript using the push method?
Read on to discover how to effectively use the push() method in JavaScript to add multiple elements to an array.
This comprehensive guide covers everything from the basic syntax and parameters of the push() method to practical examples.
What is JavaScript array push() method?
The push() method in JavaScript is used to add one or more elements to the end of an array.
It alters the array’s length by the quantity of newly added elements.
In other words, the push() method in JavaScript is a handy tool when you’re working with arrays. It allows you to add new items to the end of an array.
Syntax
array.push(element0, element1, ..., elementX) ✅
This syntax demonstrates that the push() method has the capability to accept multiple elements (element0, element1, …, elementX).
Each argument corresponds to an element you wish to append or add to the end of the array.
Parameters
element0, element1, …, elementX
These are the elements that you want to add to the end of the array. You have the option to specify one or multiple elements.
Return value
This method returns the new length of an array right after the elements have been added to your array.
It simply means that after you use push(), you’ll get a number that represents the total number of elements in the array.
Let’s say you have an array of your favorite subjects, like this:
let subjects = ["Programming", "Software Engineering"];
Now, you’ve recently taken up Web Development and Software Development, and you want to add these to your list of favorite subjects.
You can do this using the push() method:
subject.push("Web Development", "Software Development");
After running this code, your sports array will now look like this:
["Programming", "Software Engineering", "Web Development", "Software Development"];
The push() method has also another neat feature: it returns the new length of the array. So if you do:
let newLength = subject.push("Web Development", "Software Development");
The newLength variable will hold the value 4, which is the new length of the subjects array.
Here’s the complete example:
let subjects = ["Programming", "Software Engineering"];
let newLength = subjects.push("Web Development", "Software Development");✅
console.log(subjects);
console.log(newLength );
Output:
[
'Programming',
'Software Engineering',
'Web Development',
'Software Development'
]
4 (new length )
You can also use the push() method with spread syntax to merge two arrays:
const subjects = ["English", "Math"];
const moresubjects = ["Science", "Programming"];
// Merge the second array into the first one
subjects.push(...moresubjects);
console.log(subjects);
In this case, all elements from the moresubjects array are added to the end of the subjects array.
One thing to remember is that the push() method changes the original array. If you don’t want to change the original array, you might want to look into other methods like concat().
Also, while push() can technically work on any object with a length property and integer-keyed properties, it’s not suitable for strings as they are immutable.
How to push multiple elements in array JavaScript?
You can add multiple elements to an array using push() method in JavaScript.
Here’s an example code on how you can do it:
const subjects = ["English", "Math"];
subjects.push("Science", "Programming", "Software Development");✅
console.log(subjects);
In this example, “Science,” “Programming,” and “Software Development” are added to the end of the subjects array.
The push() method can take multiple arguments, and each argument will be added to the array in order.
Output:
[ 'English', 'Math', 'Science', 'Programming', 'Software Development' ]
Conclusion
In conclusion, the push() method in JavaScript, is one of the most powerful tools for manipulating arrays.
It allows you to add one or more elements to the end of an array, and it returns the new length of the array.
This guide has shown you how to use the push() method to add single or multiple elements to an array and how to merge two arrays.
We are hoping that this article provides you with enough information that help you understand the push in array JavaScript.
If you want to dive into more JavaScript topics, check out the following articles:
Thank you for reading Itsourcecoders 😊.