JavaScript Array Push

JavaScript Array Push

2 mins read287 Views Comment
Chanchal
Chanchal Aggarwal
Senior Executive Content
Updated on Dec 18, 2023 18:51 IST

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.

2022_11_Array-Push.jpg

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

  1. JavaScript Array Push
  2. Additions of Items
  3. Merger of Arrays

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)
Copy code

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);
Copy code

Output:

["English", "Hindi", "Maths", "Science", “Economics”, “Physics”]  
15+ JavaScript Array Methods with Examples
15+ JavaScript Array Methods with Examples
JavaScript array methods offer powerful tools for manipulating arrays. Key methods include 'push()' and 'pop()' for adding/removing elements at the end, 'shift()' and 'unshift()' for operations at the beginning, 'map()'...read more
JavaScript Array Sort
JavaScript Array Sort
The JavaScript array.sort() method sorts the elements of an array in place and returns the sorted array. By default, it sorts elements as strings in ascending order, but a custom...read more
How to Find the JavaScript Array Length
How to Find the JavaScript Array Length
The length property of a JavaScript array returns the number of elements in the array, providing an easy way to determine its size. It's dynamic, meaning it updates as elements...read more

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 one
colors.push(...moreColors);
console.log(colors);
Copy code

Output:

  ['white’,'red', 'green', ‘black’']

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()
Copy code
method adds one or more elements to the end of an array and returns the new length of the array. It directly modifies the original array.

Can push() add multiple elements at once?

Yes,

push()
Copy code
can add multiple elements to an array in a single call. For example,
array.push(element1, element2, element3)
Copy code
adds three elements to
array
Copy code
.

Does push() alter the original array or return a new one?

push()
Copy code
modifies the original array by adding new elements to it. It does not create a new array.

What does push() return?

push() returns the new length of the array after the elements have been added.

About the Author
author-image
Chanchal Aggarwal
Senior Executive Content

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