How to add an element to the end of an array in JavaScript

Home / JavaScript / How to add an element to the end of an array in JavaScript


How to add an element to the end of an array in JavaScript

You can add one or more elements to the end of a JavaScript array with the push() method.
The method will return the new length of the array.

Syntax

Parameters

element1 … elementN

The elements to add to the end of the array.

Return value

The new length property of the object upon which the method was called.

Examples

This is a basic example that shows how to add a single element at the end of an array.

You can also add multiple elements at once to an array:

Adding another array with push, however, adds the array as a whole and does not add the separate elements to the array:

If you want to add all elements af an array to the end of another array, you can use the concat() method.

Conclusion

In this article we have seen that you can add one or more distinct elements to the end of a JavaScript array with the push() method but you cannot merge another array into it with this method. Merging a complete array into another one can be done with the concat() method.

See also