Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to add elements to an array in JavaScript

Getting Started with Arrays in JavaScript

An array, in JavaScript, is like a list of items. You can think of it as a cupboard with several shelves. Each shelf can hold a different item. In programming terms, an array is a special variable that can hold more than one value at a time.

Now, the main question is how to add elements to this cupboard, or in our terms, an array.

The Basics of Adding Elements to an Array

There are multiple ways to add elements to an array in JavaScript. Let's start with the simplest one - the push() method.

let fruits = ["apple", "banana", "mango"];

If you run this code, you would get the output ["apple", "banana", "mango", "orange"]. The push() method, as the name suggests, pushes an element to the end of the array. It's like adding a new item on the last empty shelf of our cupboard.

Using the unshift() Method

Another method to add an element to an array is the unshift() method. The unshift() method works similarly to the push() method but it adds the new element at the beginning of the array.

let fruits = ["apple", "banana", "mango"];

The output of this code would be ["orange", "apple", "banana", "mango"]. It's like adding a new item on the top shelf of our cupboard, shifting all other items one shelf down.

Inserting an Element at a Specific Position

What if you want to add an item not at the beginning or the end, but at a specific position in the array? For this, we use the splice() method.

let fruits = ["apple", "banana", "mango"];
fruits.splice(1, 0, "orange");

In this example, the output would be ["apple", "orange", "banana", "mango"]. The splice() method can take several arguments. The first argument is the position where you want to add the element, the second argument is the number of elements you want to remove (in this case, 0), and the third argument is the element you want to add.

Adding Multiple Elements

All the methods we've discussed so far can be used to add multiple elements as well. You just need to pass the elements you want to add as arguments.

let fruits = ["apple", "banana", "mango"];
fruits.push("orange", "grapes");

The output would be ["apple", "banana", "mango", "orange", "grapes"].

Similarly, you can use unshift() and splice() to add multiple elements.

Adding Elements to an Array of Objects

In JavaScript, an array can hold any type of values including objects. An object, think of it like a container that can hold multiple values labeled by keys, in JavaScript is like a more complex version of an array.

Let's say we have an array of objects where each object is a fruit with a name and color property.

let fruits = [
  { name: "apple", color: "red" },
  { name: "banana", color: "yellow" },
  { name: "mango", color: "orange" },

Adding an object to this array is the same as adding any element. We just need to pass an object instead of a simple value.

fruits.push({ name: "grapes", color: "purple" });

The output would be

  { name: "apple", color: "red" },
  { name: "banana", color: "yellow" },
  { name: "mango", color: "orange" },
  { name: "grapes", color: "purple" },


And there you have it! We've explored how to add elements to an array in JavaScript using different methods. Each of these methods is like a different way to add items to our cupboard. Whether you need to add items to the top shelf, bottom shelf, or somewhere in the middle, JavaScript has a method for it.

Remember, programming is like cooking. You can follow a recipe to the letter, but you won't become a great chef until you start experimenting with the ingredients and cooking methods. So, don't stop here. Try these methods with different types of arrays, combine them, and see what happens. Happy coding!