1. javascript
  2. /references
  3. /array-push

push() Array Method

Use push() to add one or more elements to the end of an array. It will also return the length of the new array.

Here is a quick example:

let fruits = ['apple', 'banana'];

fruits.push('kiwi', 'mango');
console.log(fruits); 
// Output: ['apple', 'banana', 'kiwi', 'mango']

Alternatively, instead of just pushing new elements into the array, you could assign the push to a variable like so and retrieve it's new length at the same time as pushing the new elements into the array:

let fruits = ['apple', 'banana'];
let fruits_length = fruits.push('kiwi', 'mango');

console.log(fruits_length); 
// Output: 4

console.log(fruits); 
// Output: ['apple', 'banana', 'kiwi', 'mango']

Syntax and Parameters

array.push(element1[, ...[, elementN]])
ParameterDescriptionOptional/Required
element1The element to add to the end of the array.Required
elementNAdditional elements to add to the end of the array.Optional

push() method examples:

Add one item to the end of an array

let numbers = [1, 2];

numbers.push(3);
console.log(numbers);
// Output: [1, 2, 3]

Add multiple items to the end of an array

let people = ['Alice', 'Bob'];

people.push('Sally', 'Jane');
console.log(people);
// Output: ['Alice', 'Bob', 'Sally', 'Jane']

Other array methods that you may want to investigate include:

pop() - Removes the last element from an array and returns it.

shift() - Removes the first element from an array and returns it.

unshift() - Adds one or more elements to the beginning of an array and returns the new length of the array.

splice() - Adds or removes elements from an array.

slice() - Extracts a section of an array and returns a new array.