Open In App

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.




let myArray = [1, 2, 3];
let newElement = 4;
 
myArray.push(newElement);
 
console.log(myArray); // Output: [1, 2, 3, 4]

Output
[ 1, 2, 3, 4 ]
Article Tags :