The Wayback Machine - https://web.archive.org/web/20241001173620/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.


Previous Article
Next Article

Similar Reads

Difference Between Git Push Origin and Git Push Origin Master
Understanding the difference between git push origin and git push origin master is important for efficient version control in Git. These commands are used to upload changes from your local repository to a remote repository, but they function differently. This article will explain these differences, how each command works, and when to use them. What
3 min read
Alternatives of push() method in JavaScript
The task is to perform a push operation without using the push() method with the help of JavaScript. There are two approaches that are discussed below. Approach 1: Use the length property to insert the element at the end of the array. Example: This example implements the above approach. C/C++ Code <body style="text-align: center;">
2 min read
What is the difference between unshift() and Push() method in JavaScript?
JavaScript Unshift() method is very much similar to the push() method but the difference is that the unshift() method adds the elements at the very beginning of the array whereas the push() method adds at the end of the array. Javascript Array unshift() method: The JavaScript Array unshift() Method is used to add one or more elements to the beginni
2 min read
What is the use of the push() method in JavaScript Arrays ?
In JavaScript, the push() method is used to add one or more elements to the end of an array. It modifies the original array by appending the specified values. This can be particularly useful when you want to dynamically expand the size of an array or simply add new elements to the existing list. Example: Here, we are pushing newElement in myArray.
1 min read
How to Push an Object into an Array using For Loop in JavaScript ?
JavaScript allows us to push an object into an array using a for-loop. This process consists of iterating over the sequence of values or indices using the for-loop and using an array manipulation method like push(), to append new elements to the array. We have given an empty array, and we need to push the object into the array. Below is an example
3 min read
How to push an array into the object in JavaScript ?
In JavaScript, you can easily add elements to an array using the push() function. This function allows you to add one or more elements to the end of an array and returns the new length of the array. In this article, we’ll explore how to use the push() function to insert an entire array into an object, which is a useful technique for organizing data
2 min read
TypeScript Array push() Method
The Array.push() method in TypeScript is a built-in function used to append one or more elements to the end of an array. It modifies the original array and returns the new length of the array. SyntaxHere is the syntax for using the push() method: array.push(element1, ..., elementN)Parameter: This method accept a single parameter as mentioned above
2 min read
How to Push an Object into Another Object in JavaScript ?
In JavaScript, the concept of pushing an object into another object involves embedding one object within another as a property. This technique is useful for organizing data structures where related information is grouped, enhancing the ability to manage and manipulate complex data sets effectively. There are several ways to push an object into anot
4 min read
How to use push() & pop() in JavaScript Arrays?
Arrays are dynamic data structures in JavaScript that have the ability to hold several values of various types. Push() and pop() are used for adding and removing elements from an array's last index. Below are the approaches to use push() and pop() methods in JavaScript: Table of Content Basic UsageConditional UsageApproach 1: Basic UsageIn this app
2 min read
React Suite Notification toaster.push Method
React Suite is a library of React components, sensible UI design, and a friendly development experience. It is supported in all major browsers. It provides pre-built components of React which can be used easily in any web application. In this article, we'll learn about React Suite Notification toaster.push() method. This method is basically used to
3 min read
How to push elements in an array using AngularJS ?
Given an array and the task is to perform the push operation on the array using AngularJS. The arr.push() method is used to push one or more values into the array, & new values will add at the end of an array. This method changes the length of the array by the number of elements added to the array & returns the new length of the modified ar
2 min read
How to Push Data to Array Asynchronously & Save It in Node.js ?
Node.js is a powerful environment for building scalable and efficient applications, and handling asynchronous operations is a key part of its design. A common requirement is to collect data asynchronously and store it in an array, followed by saving this data persistently, for instance, in a database or a file. This article explores the techniques
6 min read
How to Push Item From an Array in Mongoose?
In Mongoose, pushing an item to an array can be done using different approaches. To push an item into an array, you can use the $push operator along with the updateOne() or updateMany() method. We will discuss the different methods to push items from an array in Mongoose: Table of Content Inserting a new document Using $push OperatorUsing addToSet
5 min read
Backbone.js push Collection
In this article, we will see the Backbone.js push Collection. The Backbone.js push Collection can be utilized to add the model to the collection's end i.e. it simply pushes the model to the collection. Syntax: collection.push(models, options) Parameters: It will take 2 parameters, which are described below: models: This parameter value is used to s
3 min read
PHP | Ds\Sequence push() Function
The Ds\Sequence::push() function is an inbuilt function in PHP which adds values to the end of the sequence. Syntax: void abstract public Ds\Sequence::push( $values ) Parameters: This function accepts single parameter $values which contains one or more values. It hold the value to be added in the sequence. Return value: This function does not retur
2 min read
PHP | Ds\Vector push() Function
The Ds\Vector::push() function is an inbuilt function in PHP that is used to add elements to the end of the vector. Syntax: void public Ds\Vector::push( $values ) Parameters: This function accepts a single parameter $values which contains one or more than one element to be added to the vector. Return Value: This function does not return any values.
2 min read
PHP | Ds\Deque push() Function
The Ds\Deque::push() function is an inbuilt function in PHP which is used to add the elements to the Deque by appending an element at the end of the Deque. Syntax: public Ds\Deque::push( $values ) : void Parameters: This function accepts single parameter $values which holds the elements to be added to the Deque. Return Value: This function does not
2 min read
PHP | Ds\Stack push() Function
The Ds\Stack::push() function of PHP is used to add elements at the end of the stack. That it is used to push elements on to a stack. The elements being pushed can be of mixed data types. Syntax: void public Ds\Stack::push ($values) Parameter: This function accepts a single parameter $values which is used to push onto the stack. Return Value: This
1 min read
PHP Ds\PriorityQueue push() Function
The Ds\PriorityQueue::push() Function in PHP is used to push or insert values in a PriorityQueue instance. This function pushes values along with a given priority in the PriorityQueue. Syntax: void public Ds\PriorityQueue::push($value, $priority) Parameters: This function accepts two parameters: $value: This is the value to be inserted in the Prior
1 min read
PHP Ds\Queue push() Function
The Ds\Queue::push() Function in PHP is used to push or insert values in a PriorityQueue instance. This function can also insert a list of values directly to the Queue. Syntax: void public Ds\Queue::push($value1, $value2, .... $valueN) Parameters: This function accepts a list of values separated by spaces as parameter. All of these values are pushe
1 min read
Sending Push Notifications : Progressive Web Apps
We need permissions to send push notifications. Key Points : Notifications must be timely. Should be precise (contains all the information a user needs). Should be relevant to the user. Basic Example : An airline app must send push notifications such as "Your flight will be boarding from..." Browser that supports push notifications are : Chrome 44
3 min read
PHP SplDoublyLinkedList push() Function
The SplDoublyLinkedList::push() function is an inbuilt function in PHP which is used to push an element at the end of the doubly linked list. Syntax: void SplDoublyLinkedList::push( $value ) Parameters: This function accepts a single parameter $value which holds an element to push it at the end of doubly linked list. Return Value: It does not retur
1 min read
p5.js | Push Operation on Stack
What is Stack? The stack is a linear data structure like an array in which only the last inserted element can be accessed. This data structure works over the principle of Last In First Out (LIFO). It is very fast since it has to access only the last added element so it requires constant time to do so having complexity O(1). Stacks should be used ov
3 min read
Node.js push() function
push() is an array function from Node.js that is used to add elements to the end of an array. Syntax: array_name.push(element) Parameter: This function takes a parameter that has to be added to the array. It can take multiple elements also. Return type: The function returns the array after adding the element. The program below demonstrates the work
1 min read
How to use .join() with push and replace together?
Array.join() MethodThe array.join() method is an inbuilt function in JavaScript which is used to join the elements of an array into a string.The elements of the string will be separated by a specified separator and its default value is comma (, ). Syntax: array.join(separator) Array.push() Method The array.push() function is used to push one or mor
2 min read
Collect.js | push() Function
The push() function is used to add a new value to the end of the collection. In JavaScript, the array is first converted to a collection and then the function is applied to the collection. Syntax: data.push(value) Parameters: This function accepts a single parameter as mentioned above and described below: value: This parameter holds the value to be
1 min read
How to push data in a MongoDB document ?
The insertOne() and insertMany() are the two methods of the MongoDB module in node.js that are used to push the documents in the collections of the MongoDB database. The insertOne() method inserts one data at a time into the collection and insertMany() method inserts multiple data into the collection of the MongoDB database. In this article, we wil
2 min read
Semantic-UI Sticky Push
Semantic UI open-source framework gives icons or glyphs that are used to show pictures related to some elements using CSS and jQuery that is used to create great user interfaces. It is a development framework used to create beautiful and responsive layouts. Semantic UI Sticky content remains fixed to the browser viewport until another content on th
2 min read
React Suite Grid Push and Pull
A React suite is a library of React components, sensible UI design, and a friendly development experience. It is supported in all major browsers. It provides pre-built components of React which can be used easily in any web application. In this article, we'll learn about React suite Grid Push and Pull. The grid push and pull are used to change the
5 min read
How to send push notification using XMPP Server?
In this article, we are going to learn how can we send push notifications using the XMPP server. XMPP stands for extensible Messaging and Presence Protocol and it is a free open-source, cross-platform web server solution stack package. which is easy to install and contains mainly the Apache HTTP Server, and MariaDB database, and it is written in pr
2 min read