push And pop Examples

push

The general syntax for this method is:

arrayName.push(item1, item2, ...)

This method adds one or more items to the END of an array and returns the new length.

The new items may be of any data type.

Example

1let arr = ['a', 'b', 'c'];
2
3console.log(arr.push('d', 'f', 42));
4
5console.log(arr);

Output

6
['a', 'b', 'c', 'd', 'f', 42]

pop

The general syntax for this method is:

arrayName.pop()

This method removes and returns the LAST element in an array.

No arguments are placed inside the parentheses ().

Example

1let arr = ['a', 'b', 'c', 'd', 'e'];
2
3arr.pop();
4
5console.log(arr);

Output

e
['a', 'b', 'c', 'd']