How to use push() Method In Javascript

The JavaScript Array push() Method is used to add one or more values to the end of the array. This method changes the length of the array by the number of elements added to the array.

Syntax:

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

Example: In this example, we are using the push() method to add a new element in the last index of the array.

Javascript
const num1 = [1, 2, 3];
num1.push(4);
console.log(num1);

// adding multiple element i the last index of array
const num2 = [10, 20, 30, 40, 50];
num2.push(60, 70);
console.log(num2);

Output
[ 1, 2, 3, 4 ]
[
  10, 20, 30, 40,
  50, 60, 70
]

How to Add Elements to the End of an Array in JavaScript ?

Appending elements to the end of an array is a fundamental task in JavaScript, essential for data manipulation and management. This guide will provide an overview of various methods to achieve this, enhancing your coding efficiency.

Similar Reads

Importance of Adding Elements to an Array

Adding elements to an array is crucial for:...

1. Using push() Method

The JavaScript Array push() Method is used to add one or more values to the end of the array. This method changes the length of the array by the number of elements added to the array....

2. Using Spread (…) Operator

Using the spread (…) operator, you can add elements to the end of an array by creating a new array and combining the original array with the new elements....

3. Using concat() Method

In this approach, Using the concat() method, create a new array by combining the original array with additional elements. It does not modify the original array, giving a clean way to add elements....

4. Using the length Property

In this approach, we are Using the length property, you can add elements to the end of an array by assigning values to indexes equal to the current length, effectively extending the array with new elements....

5. Using splice() Method

Using the splice() method, you can add elements to the end of an array by specifying arr.length as the index and setting the number of elements to remove to 0. It directly modifies the original array....

6. Using a loop

Using a loop, you can add multiple elements to the end of an array in JavaScript. Iterate through the elements to be added and assign each to the next available index in the array using its length. This appends the new elements sequentially....