The JavaScript array push() method adds one or more elements to the end of the given array.
JavaScript Array push() method changes the length of the original array.
The push()
method adds new items to the end of an array.
The push()
method returns the new length.
The push
method appends values to an array.
Contents
hide
Syntax
array.push(item1, item2, ..., itemN)
Parameters
item1, item2..itemN – The elements to be added. Minimum one item is required.
Return Value
A number stating the new length of the array.
Examples
const animals = ['pigs', 'goats', 'sheep'];
const count = animals.push('cows');
console.log(count);
// expected output: 4
console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows"]
animals.push('chickens', 'cats', 'dogs');
console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows", "chickens", "cats", "dogs"]
let sports = ['soccer', 'baseball']
let total = sports.push('football', 'swimming')
console.log(sports) // ['soccer', 'baseball', 'football', 'swimming']
console.log(total) // 4
let newArray = [1, 2, 3, 4, 5].push(6)
// [1, 2, 3, 4, 5, 6]
The .push() method mutates the old array.