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
JavaScript Array Sort
How to Find the JavaScript Array Length

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’']
Recommended online courses

Best-suited JavaScript courses for you

Learn JavaScript with these high-rated online courses

2.05 K
3 months
– / –
6 months
– / –
2 months
– / –
100 hours
– / –
100 hours
– / –
6 months
– / –
1 month
– / –
40 hours
– / –
40 hours

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