JavaScript Array Push
The JavaScript array.push() method adds one or more elements to the end of an array, directly modifying the original array. It's widely used for dynamically expanding arrays, and conveniently returns the new length of the array after the elements have been added, making it useful for tracking array size.
In this blog, we will cover one such method of a JavaScript array, that is array push. Here we will explore the meaning of JavaScript array push with the help of relevant examples. Also, you will learn how various functions can be performed in the array push method of JavaScript. Let’s get started without further ado.
Table of Contents
Explore: Check Free JavaScript Courses Online
JavaScript Array Push
JavaScript array push adds one or more items to the end of the array. It changes the existing length of the array and gives it a new length.
Syntax:
push(item0) push(item0, item1) push(item0, item1, /* … ,*/ itemN)
Parameters:
itemN
The item(s) to add to the end of the array.
Return Value
It is the new length of the array after the addition of a new item in it.
Let’s understand it with the help of an example.
Adding Items to an Array
const subjects = ["English", "Hindi", "Maths", "Science"];subjects.push("Economics");console.log(subjects)
Output:
["English", "Hindi", "Maths", "Science", “Economics”]
Similarly, you can add two or more items to the existing arrays and get a return value, i.e, output with a new array length.
const subjects = ["English", "Hindi", "Maths", "Science"];subjects.push("Economics" , “Physics”);console.log(subjects);
Output:
["English", "Hindi", "Maths", "Science", “Economics”, “Physics”]
Merging Items to an Array
In this example, we use spread syntax to push all items from a second array into the first array.
const colors = ['white', 'red'];const moreColors = ['green', 'black'];
// Merge the second array into the first onecolors.push(...moreColors);
console.log(colors);
Output:
['white’,'red', 'green', ‘black’']
Best-suited JavaScript courses for you
Learn JavaScript with these high-rated online courses
Conclusion
In the above blog, we have covered JavaScript array push using which one can add more items to an array. It returns the new array length with the addition of new arrays pushed into it. Also, merging of arrays, push() function to non-array objects, and an object in non-array form is explained in this blog.
Explore more- Top JavaScript interview questions and answers
Top FAQs on JavaScript Array Push
What does the push() method do in JavaScript?
The
push()
Can push() add multiple elements at once?
Yes,
push()
array.push(element1, element2, element3)
array
Does push() alter the original array or return a new one?
push()
What does push() return?
push() returns the new length of the array after the elements have been added.
Chanchal is a creative and enthusiastic content creator who enjoys writing research-driven, audience-specific and engaging content. Her curiosity for learning and exploring makes her a suitable writer for a variety ... Read Full Bio