.push()

Adds one or more elements to end of array and returns new length.

Syntax

The .push() method is not to be confused with returning an entirely new array with the passed object.

array.push(item);

Multiple elements can be passed to end of array.

array.push(item1, item2);

Examples

Add one item to end of colors array:

const colors = ['red', 'orange'];
colors.push('yellow');
// Output: 3
console.log(colors);
// Output: ['red', 'orange', 'yellow']

Add multiple items to end of colors array:

const colors = ['red', 'orange', 'yellow'];
colors.push('green', 'blue');
// Output: 5
console.log(colors);
// Output: ['red', 'orange', 'yellow', 'green', 'blue']
Edit this page on GitHub

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your feedback.

Learn JavaScript on Codecademy

Edit this page on GitHub

Contributors