How to Append an Item to an Array in JavaScript
On this page, you can learn the ways of appending an item or multiple items to an array in JavaScript.
In this tutorial, you will find out the solutions that JavaScript offers for appending an item to an array.
To append a single item to an array, use the <kbd class="highlighted">push()</kbd> method provided by the array object. You can do this as follows:
Appending a single item with push()
Note that <kbd class="highlighted">push()</kbd> modifies the original array.
To create a new array without modifying the original, use the concat() method as follows:
Appending an item with concat()
Note that <kbd class="highlighted">concat()</kbd> does not modify the original array. Instead, it returns a new array that can be assigned to a variable:
Reassigning the result
To append multiple items, you can pass several arguments to <kbd class="highlighted">push()</kbd>:
Appending multiple items with push()
You can also pass a list of items separated by commas to <kbd class="highlighted">concat()</kbd>:
Appending multiple items with concat()
You can also pass an array:
Passing an array to concat()
As with the previous example, this method returns a new array without mutating the original.
Describing Arrays
JavaScript arrays are a convenient way to store multiple values in a single variable. Unlike standard variables, an array can hold more than one value simultaneously. Arrays are similar to objects in that both support trailing commas.
Elements inside an array can be of different types, such as strings, booleans, objects, or even other arrays. This means you can create an array with a string in the first position, a number in the second, and so on.