JavaScript Tutorials

JavaScript Array push()
Syntax & Examples

Array.push() method

The push() method of the Array class in JavaScript adds one or more elements to the end of an array and returns the new length of the array.


Syntax of Array.push()

There are 4 variations for the syntax of Array.push() method. They are:

1.
push()

This method adds no elements to the array and returns the new length of the array.

Returns value of type Number.

2.
push(element1)

Parameters

ParameterOptional/RequiredDescription
element1requiredThe first element to add to the end of the array.

This method adds one element to the end of the array and returns the new length of the array.

Returns value of type Number.

3.
push(element1, element2)

Parameters

ParameterOptional/RequiredDescription
element1requiredThe first element to add to the end of the array.
element2requiredThe second element to add to the end of the array.

This method adds two elements to the end of the array and returns the new length of the array.

Returns value of type Number.

4.
push(element1, element2, /* …, */ elementN)

Parameters

ParameterOptional/RequiredDescription
element1requiredThe first element to add to the end of the array.
element2requiredThe second element to add to the end of the array.
elementNoptionalAdditional elements to add to the end of the array.

This method adds one or more elements to the end of the array and returns the new length of the array.

Returns value of type Number.



✐ Examples

1 Using push() method to add one element to an array

In JavaScript, we can use the push() method to add one element to the end of an array and return the new length of the array.

For example,

  1. We define an array variable arr with elements [1, 2, 3].
  2. We use the push() method to add the element 4 to the end of the array.
  3. The new length of the array is stored in the variable newLength.
  4. We log newLength and the updated array to the console using console.log() method.

JavaScript Program

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

Output

4
[1, 2, 3, 4]

2 Using push() method to add multiple elements to an array

We can use the push() method to add multiple elements to the end of an array and return the new length of the array.

For example,

  1. We define an array variable arr with elements ['a', 'b', 'c'].
  2. We use the push() method to add the elements 'd' and 'e' to the end of the array.
  3. The new length of the array is stored in the variable newLength.
  4. We log newLength and the updated array to the console using console.log() method.

JavaScript Program

const arr = ['a', 'b', 'c'];
const newLength = arr.push('d', 'e');
console.log(newLength);  // Output: 5
console.log(arr);       // Output: ['a', 'b', 'c', 'd', 'e']

Output

5
['a', 'b', 'c', 'd', 'e']

3 Using push() method to add elements to an empty array

We can use the push() method to add elements to an empty array and return the new length of the array.

For example,

  1. We define an empty array variable arr.
  2. We use the push() method to add the elements 1, 2, and 3 to the end of the array.
  3. The new length of the array is stored in the variable newLength.
  4. We log newLength and the updated array to the console using console.log() method.

JavaScript Program

const arr = [];
const newLength = arr.push(1, 2, 3);
console.log(newLength);  // Output: 3
console.log(arr);       // Output: [1, 2, 3]

Output

3
[1, 2, 3]

Summary

In this JavaScript tutorial, we learned about push() method of Array: the syntax and few working examples with output and detailed explanation for each example.