Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to add to an array in JavaScript

Understanding Arrays in JavaScript

An array is like a list that we use in our day-to-day life. Imagine a shopping list where you note down the items you need to buy from the grocery store. In JavaScript, we can create a similar list called an array. An array holds multiple values in a single variable, and these values can be accessed by their index number.

For example:

let shoppingList = ['Apple', 'Orange', 'Banana'];

In the above example, we have an array shoppingList with three items inside it.

Adding Elements to an Array

Just like how you might realize you've forgotten to add 'Milk' to your shopping list and need to add it, sometimes you'll need to add new elements to a JavaScript array. JavaScript provides us with several methods to add new elements to an array. Here, we'll see the most commonly used methods.

Using the push() Method

The push() method is like tacking an item onto the end of your shopping list. It adds new items to the end of an array, and returns the new length of the array.

Here's how we can use push() to add 'Milk' to our shoppingList:

shoppingList.push('Milk');

After executing the above line, our shoppingList will look like this: ['Apple', 'Orange', 'Banana', 'Milk'].

Using the unshift() Method

The unshift() method is like squeezing an item into the top of your list. It adds new items to the beginning of an array, and returns the new length of the array.

Let's add 'Bread' to the beginning of our shoppingList:

shoppingList.unshift('Bread');

Now, our shoppingList looks like this: ['Bread', 'Apple', 'Orange', 'Banana', 'Milk'].

Using the splice() Method

The splice() method is like inserting an item into the middle of your list. It adds and/or removes items from an array, and returns the removed item(s). The first parameter defines the position where new elements should be added (spliced in).

Let's add 'Butter' to the middle of our shoppingList:

shoppingList.splice(2, 0, 'Butter');

Now, our shoppingList looks like this: ['Bread', 'Apple', 'Butter', 'Orange', 'Banana', 'Milk'].

Checking the Length of an Array

You might want to know how many items you have in your list. Similarly, the length property returns the number of elements in an array.

console.log(shoppingList.length);

After running the above line, it will print 6 to the console, because we have six items in our shoppingList.

Conclusion

Think of arrays as your shopping lists when you're coding. You can keep adding items (elements) to your list (array) as needed, either at the end, the beginning, or even in the middle of your list. The methods push(), unshift(), and splice() are your tools to do just that in JavaScript. And don't forget that you can always check how many items are in your list using the length property.

Remember, the key to becoming a proficient programmer is practice. Try creating your own arrays and adding elements to them. Happy coding!