How to Declare an Empty Array in JavaScript

Declare an Empty Array in JavaScript

In this tutorial, we will walk through different methods, understand when to use them, and look at live examples that demonstrate exactly what happens.

Method 1: Using Array Literal Syntax

This is the most straightforward and widely-used method to declare an empty array. It uses square brackets, which is both minimal and highly readable — perfect for beginners and pros alike.

const fruits = [];

console.log(fruits);
console.log("Length:", fruits.length);
[]
Length: 0

Method 2: Using the Array Constructor

Another way to create an empty array is by using the built-in Array constructor. While not as common for empty arrays, it can still be handy — especially if you're dynamically building arrays or using constructor patterns in your codebase.

const emptyList = new Array();

console.log(emptyList);
console.log("Length:", emptyList.length);
[]
Length: 0

Method 3: Declaring and Assigning Later

Sometimes, you'll just want to declare a variable first, then assign an empty array to it later. This is helpful in cases where the array's usage isn't known immediately, but you want to establish the variable early in your code for clarity or scope control.

let tasks;
tasks = [];

console.log(tasks);
[]

Method 4: Using Array.of() With No Arguments

Although Array.of() is typically used to create arrays with initial values, calling it without arguments will return an empty array. This isn't the most conventional approach, but it's good to know for completeness.

const blank = Array.of();

console.log(blank);
[]

When to Use Each Method

So how do you choose which method to use? That depends on context and coding style:

  • Use literal syntax ([]) when you want a fast, clean, and highly readable solution — this covers 95% of use cases.
  • Use the constructor when integrating with patterns or legacy code that relies on object instantiation.
  • Declare and assign later if you're separating variable declarations from their usage, especially in larger functions or scoped blocks.
  • Array.of() is more academic in this case — it's nice to know, but not widely adopted for empty arrays.

Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...