The Wayback Machine - https://web.archive.org/web/20250114165705/https://www.geeksforgeeks.org/javascript-array-push-method/
Open In App

JavaScript Array push() Method

Last Updated : 09 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The push() method in JavaScript adds one or more elements to the end of an array and returns the new length of the array. It modifies the original array, increasing its length by the number of elements added.

Syntax

arr.push(element0, element1, … , elementN);

Parameters

This method contains as many numbers of parameters as the number of elements to be inserted into the array. 

Return value

A Number – New length of the array.

Example:  In this example, function func() initializes an array with elements ‘GFG’, ‘gfg’, ‘g4g’, then pushes the string GeeksforGeeks to the end of the array using the push() method. Finally, it logs the updated array to the console.

JavaScript
function func() {
    let arr = ['GFG', 'gfg', 'g4g'];
    // Pushing the element into the array
    arr.push('GeeksforGeeks');
    console.log(arr);
}
func();

Output
[ 'GFG', 'gfg', 'g4g', 'GeeksforGeeks' ]

Example: In this example we use The function func() initializes an array `arr`, adds elements [23, 45, 56]` to it using push(), then logs the modified array to the console.

JavaScript
function func() {
    // Original array
    let arr = [34, 234, 567, 4];

    // Pushing the elements
    arr.push(23, 45, 56);
    console.log(arr);
}
func();

Output
[
  34, 234, 567, 4,
  23,  45,  56
]

JavaScript Array push() Method UseCase

1. What is the use of the push() method in JavaScript Arrays

The `push()` method in JavaScript arrays is used to add one or more elements to the end of an array. It modifies the original array by appending the new elements and returns the updated length of the array.

2. How to push an array into the object in JavaScript ?

The array push() function adds one or more values to the end of the array and returns the new length. This method changes the length of the array. An array can be inserted into the object with push() function.

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.

Supported Browsers:

JavaScript Array push() method- FAQs

What is array push () method in JavaScript?

The push() method in JavaScript adds one or more elements to the end of an array.

What does the push() method return?

The push() method in JavaScript returns the new length of the array after adding the specified element(s) to the end of the array.

Does push() modify the original array?

Yes, the push() method modifies the original array by adding one or more elements to its end. It does not create a new array but rather extends the existing one with the provided elements.

Can you use push() with multiple arguments?

The JavaScript push() method is used to add one or more elements to the end of an array and returns the new length of the array. While push() is typically used to add a single element at a time, you can indeed use it to push multiple elements simultaneously by passing those elements as separate arguments.

How does push() handle NaN values?

The push() method treats NaN (Not a Number) values like any other value and adds them to the array without any special handling or modification.

Become an expert in solving problems with DSA JavaScript—the course designed to teach you Data Structures and Algorithms using JavaScript. Over the next 90 days, dive deep into key DSA concepts, learn to optimize your code, and gain hands-on experience solving challenging problems. Whether you're a beginner or looking to refine your JavaScript skills, this course will provide the knowledge you need to succeed.
Take on the Three 90 Challenge today! Complete 90% of the course within 90 days, and you’ll earn a 90% refund. This challenge is your chance to stay motivated, improve your skills, and get rewarded for your progress. Don’t wait—start your journey to DSA mastery with JavaScript today!


Next Article

Similar Reads

three90RightbarBannerImg