JavaScript Array push() Method

The JavaScript Array push() method 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 new length of the array.

For example, array.push(element1, element2, ...) is useful when you need to add elements to the end of an array dynamically.

Syntax

The syntax for the Array.push() method is:

plaintext
array.push(element1, element2, ..., elementN)

Parameters

The Array.push() method accepts one or more parameters:

  • elementN (Required): The elements to be added to the end of the array.

Return Value

The new length of the array after the elements have been added.

Examples of JavaScript Array.push() Method

Example 1: Adding One Element to an Array

The push() method adds a single element to the end of the array.

javascript
const fruits = ['apple', 'banana'];
const newLength = fruits.push('mango');
console.log(fruits);     // Output: ['apple', 'banana', 'mango']
console.log(newLength);  // Output: 3

Explanation: The push() method adds 'mango' to the fruits array, and the new length of the array becomes 3.

Example 2: Adding Multiple Elements to an Array

You can add multiple elements to an array with a single call to push().

javascript
const numbers = [1, 2, 3];
const newLength = numbers.push(4, 5);
console.log(numbers);     // Output: [1, 2, 3, 4, 5]
console.log(newLength);   // Output: 5

Explanation: The push() method adds 4 and 5 to the numbers array, and the new length of the array becomes 5.

Example 3: Using push() to Add Elements to an Empty Array

You can initialize an empty array and add elements to it using push().

javascript
const emptyArray = [];
emptyArray.push('apple', 'banana');
console.log(emptyArray);  // Output: ['apple', 'banana']

Explanation: The push() method adds 'apple' and 'banana' to the empty array, resulting in an array with two elements.

Example 4: Using push() with Arrays of Objects

The push() method works with arrays containing objects, adding the object references to the array.

javascript
const users = [{ name: 'Alice' }];
const newUser = { name: 'Bob' };
users.push(newUser);
console.log(users);  
// Output: [{ name: 'Alice' }, { name: 'Bob' }]

Explanation: The push() method adds the new object (newUser) to the users array.

Example 5: Using push() to Append Arrays

You can use push() to append arrays within arrays, although it will not flatten them.

javascript
const arr1 = [1, 2];
const arr2 = [3, 4];
arr1.push(arr2);
console.log(arr1);  // Output: [1, 2, [3, 4]]

Explanation: The push() method adds arr2 as a single element to arr1, creating a nested array structure.

Example 6: Using push() with Spread Operator to Append Arrays

If you want to append the contents of one array to another without nesting, you can combine push() with the spread operator (...).

javascript
const arr1 = [1, 2];
const arr2 = [3, 4];
arr1.push(...arr2);
console.log(arr1);  // Output: [1, 2, 3, 4]

Explanation: The spread operator (...) expands arr2 into individual elements, which are then added to arr1 using push().

JavaScript

8950

537

Related Articles