Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to create an empty array in JavaScript

Let's Dive Into Empty Arrays in JavaScript

Starting from Scratch: What is an array?

Before we delve into creating an empty array, let us first understand what an array is. Think of an array as a collection of items. These items could be anything - numbers, strings, objects, and even other arrays. In real life, an array could be a list of your favorite movies, the names of your friends, or the collection of books on your shelf. In JavaScript, we can store these collections in arrays.

The Basics: Creating an Array in JavaScript

In JavaScript, we can create an array using square brackets []. Here's how you would typically create an array:

let myFriends = ['Alice', 'Bob', 'Charlie'];

In this example, myFriends is an array that holds three strings. But what if we want to create an array without any items? That's where empty arrays come into the picture.

The Null Set: Creating an Empty Array

An empty array is an array that does not contain any elements. It's like a shopping list you haven't written anything on yet, or a bookshelf you've just bought and haven't put any books on. To create an empty array in JavaScript, you can use the following syntax:

let emptyArray = [];

In this example, emptyArray is an array with no items. It's as simple as that! But why would you want to create an empty array? Good question!

Why Use Empty Arrays?

There are many scenarios where you might want to start with an empty array. For example, you might want to dynamically add items to an array based on user input. Starting with an empty array allows you to add items one at a time as they come in.

Imagine you are throwing a party and you're not sure who will come. You could start with an empty array called partyGuests and add guests as they RSVP.

let partyGuests = [];

partyGuests.push('Alice');
partyGuests.push('Bob');

console.log(partyGuests); // ['Alice', 'Bob']

In this example, we started with an empty array and used the push method to add guests to the array.

More Ways to Create an Empty Array

Now, while the method we've used above is the most common way to create an empty array, JavaScript provides us with a few more options. Let's explore those.

The Array Constructor

The Array constructor is a function that, well, constructs an array. It's like a machine in a factory line that creates arrays. Here's how you can use it to create an empty array:

let emptyArray = new Array();

It's important to note that while this method is perfectly valid, it's generally more common to use the square brackets method for its simplicity and readability.

A Note on Length

One interesting property of arrays in JavaScript is their length property. This tells you how many items are in the array. When you create an empty array, its length is 0, because there are no items in it.

let emptyArray = [];
console.log(emptyArray.length); // 0

Conclusion: The Power of Empty Arrays

Empty arrays in JavaScript, as we've seen, are deceptively simple. With just a pair of square brackets, you have a powerful data structure at your disposal. They are like empty canvases waiting for you to paint your data onto them. They can start off empty, but with methods like push, you can fill them with whatever data you need, whenever you need to.

Remember, arrays are just one of the many tools in your JavaScript toolbox. But understanding them deeply, including the concept of empty arrays, will help you write more flexible and powerful code. Keep practicing, keep experimenting, and as always, happy coding!