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:
push()
This method adds no elements to the array and returns the new length of the array.
Returns value of type Number
.
push(element1)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
element1 | required | The 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
.
push(element1, element2)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
element1 | required | The first element to add to the end of the array. |
element2 | required | The 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
.
push(element1, element2, /* …, */ elementN)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
element1 | required | The first element to add to the end of the array. |
element2 | required | The second element to add to the end of the array. |
elementN | optional | Additional 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,
- We define an array variable arr with elements [1, 2, 3].
- We use the push() method to add the element 4 to the end of the array.
- The new length of the array is stored in the variable newLength.
- 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,
- We define an array variable arr with elements ['a', 'b', 'c'].
- We use the push() method to add the elements 'd' and 'e' to the end of the array.
- The new length of the array is stored in the variable newLength.
- 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,
- We define an empty array variable arr.
- We use the push() method to add the elements 1, 2, and 3 to the end of the array.
- The new length of the array is stored in the variable newLength.
- 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.