Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Array.prototype.push

Appends elements to the end of an array.

Available since: ES3

Syntax

array.push(element1, element2, /* … */)

Parameters

ParameterTypeDescription
element1, …anyThe elements to append.

Return value

numberThe new length of the array.

Examples

Appending an element:

const colors = ['red'];
colors.push('green');
 
console.log(colors.join(', '));

Output:

red, green

The companion pop() removes the last element and returns it:

const stack = ['a', 'b', 'c'];
const last = stack.pop();
 
console.log(last);
console.log(stack.join(', '));

Output:

c
a, b

Notes

The method modifies the array itself. unshift() (add) and shift() (remove) work with the start of the array.

See also

Related recipes

In the tutorial

  • Arrays — What an array is in JavaScript: creating one, indexes, length, changing elements and nested arrays.

Thanks for reading!

Donate to the site — we work hard for you.