How to Append an Item to an Array in JavaScript

In this tutorial, you will find out the solutions that JavaScript offers for appending an item to an array.

Imagine you want to append a single item to an array. In this case, the push() method, provided by the array object can help you. So, it would be best if you act as follows:

w3docs logo Javascript append single item
const animals = ['dog', 'cat', 'mouse']; animals.push('rabbit'); console.log(animals);

Please, take into account that push() changes your original array.

For creating a new array, you should implement the concat() method, like this:

w3docs logo Javascript append concat item
const animals = ['dog', 'cat', 'mouse']; const allAnimals = animals.concat('rabbit'); console.log(allAnimals);

Also notice that the concat() method doesn’t add an item to the array, but makes a completely new array that can be assigned to another variable or reassigned to the original one:

w3docs logo Javascript append concat item
let animals = ['dog', 'cat', 'mouse']; animals = animals.concat('rabbit'); console.log(animals);

If you intend to append multiple items to an array, you can also use the push() method and call it with multiple arguments, like here:

w3docs logo Javascript append push single item
const animals = ['dog', 'cat', 'mouse']; animals.push('rabbit', 'turtle'); console.log(animals);

Also, you can use the concat() method passing an items’ list, separated by a comma, as follows:

w3docs logo Javascript append concat item
const animals = ['dog', 'cat', 'mouse']; const allAnimals = animals.concat('rabbit', 'turtle'); console.log(allAnimals);

Either an array:

w3docs logo Javascript append concat item
const animals = ['dog', 'cat', 'mouse']; const allAnimals = animals.concat(['rabbit', 'turtle']); console.log(allAnimals);

Notice that this method will not mutate your original array, but will return a new one.

Describing Arrays

JavaScript arrays are a super-handy means of storing multiple values in a single variable. In other words, an array is a unique variable that can hold more than a value at the same time. Arrays are considered similar to objects. The main similarity is that both can end with a comma.

One of the most crucial advantages of an array is that it can contain multiple elements on the contrary with other variables.

An element inside an array can be of a different type, such as string, boolean, object, and even other arrays. So, it means that you can create an array having a string in the first position, a number in the second, and more.