We're live-coding on Twitch! Join us!

Basics

This methods appends one or more value to the last position of an array. This method mutates the array returning the new length of the array.

let newArray = [1, 2, 3, 4, 5].push(6)
// [1, 2, 3, 4, 5, 6]

The push method:

  • Takes a value or values as arguments.
  • Adds the value(s) to the end of the array.
  • Returns the new length of the array.

Syntax

array.push(element)

1 Parameter

element This is the element to be added to the array

Returns the length of the array

The method will return the new length of the array. If the array is empty, it returns 0.

const names = ['Johnny', 'Pete', 'Sammy']
console.log(names.push('Larry'));
// output: 4

The .push() method mutates the old array.

Common Uses and Snippets

Appending new values to the end of an array

Adding object values to an array of objects

This is used in situations like adding todo items to a todo list.

Like this article? Follow @codebeast on Twitter