How to Declare an Empty Array in JavaScript? 4 Best Ways

An array is one of the most used functions in JavaScript. Various arrays, like copy arrays, are helpful. But before all of this, you should learn how to declare an empty array in JavaScript, as it is the foundation for learning about arrays. This guide will discuss the top four ways how to declare an empty array in JavaScript.

The Top 4 ways for are:

  • Use a New Empty Array – myArray = []; to reset the array.
  • myArray.length = 0; to clear the array.
  • myArray.splice(0, myArray.length); to remove all elements.
  • Loop with myArray.pop(); to empty the array.

Furthermore, we will discuss these methods in detail to handle the difficulties. We will also discuss why we should use empty arrays and how to declare an empty array in JavaScript. Later, you can easily convert the array to a map in JavaScript. Without further ado, let’s get started.

Why Use Empty Arrays?

There are many situations where using an empty array might benefit you. For instance, you could utilize it as a placeholder for dynamically adding data based on user input. An empty array helps you include items individually as they come in.

But how to declare an empty array in JavaScript? Let’s understand with an example. Suppose you are having a birthday party and are still determining who will come. In this situation, you can use the empty array called partyInvites and add them as they RSVP. Look at the following code:

let partyInvites = [];

partyInvites.push(‘John’);

partyInvites.push(‘James’);

console.log(partyInvites); 

In the above example, we used the push method to include guests in the array.

Methods to Empty an Existing Array? Top 4 Ways

This section will focus on the top 4 ways on how to declare an empty array in JavaScript.

Use a New Empty Array – myArray = []; to reset the array.

The first method to declare an empty array is to use a new empty array. It is one of the simplest and quickest ways to vacant your arrays. Look at the following code:

myArray = [ ]; 

When you use the above code, it will fix the size of the new array as 0, therefore emptying the array. It is recommended to use if no references in the code are directed towards the original array. Let’s understand via an example:

let myArray = [6, 7, 8, 9, 10];

console.log(“Array Elements: ” + myArray);

console.log(“Array Length: ” + myArray.length);

function vacantArray() {

    myArray = [];

    console.log(“Empty Array Elements: ” + myArray);

    console.log(“Empty Array Length: ” + myArray.length);

}

vacantArray();

Output:

Array Elements: 6,7,8,9,10

Array Length: 5

Empty Array Elements:

Empty Array Length: 0

myArray.length = 0; to clear the array.

In this method, we will use the length property, which allows us to fix the length of an array. If we set the length to 0, it will delete the array’s elements. Let’s look at the following code:

myArray.length = 0;

When we use this property and fix its length to 0, it implies that the array’s size is 0. We must remember that the other values are in the same positions but can’t be accessed when approaching the array. Due to this, the code may contain memory residuals and the values should be removed to avoid this issue. Let’s understand via an example:

let myArray = [6, 7, 8, 9, 10];

console.log(“Array Elements: ” + myArray);

console.log(“Array Length: ” + myArray.length);

function vacantArray() {

myArray.length= 0 ;

    console.log(“Empty Array Elements: ” + myArray);

    console.log(“Empty Array Length: ” + myArray.length);

}

vacantArray();

Output:

Array Elements: 6,7,8,9,10

Array Length: 5

Empty Array Elements:

Empty Array Length: 0

myArray.splice(0, myArray.length); to remove all elements.

This method will use the splice() to declare an empty array. It is used to eradicate a no. of array elements in JS, which is mentioned as a specification in the method. Let’s look at the following code:

myArray.splice(0, myArray.length);

In the above code, the first argument indicates the array index from which the eradication should start. In contrast, the second argument indicates the number of elements that should be eradicated. The first argument should always be 0 to declare an empty array.

Loop with myArray.pop(); to empty the array.

The last method to create an empty array is the pop() method. In this method, as the name suggests, every element is popped one after the other in the array in a while loop till the array is vacant. Look at the following code:

while(myArray.length > 0) {

    myArray.pop();

}

This method is straightforward but takes a lot of time as each element is popped one after the other. Let’s understand via an example:

let myArray = [6, 7, 8, 9, 10];

console.log(“Array Elements: ” + myArray);

console.log(“Array Length: ” + myArray.length);

function vacantArray() {

    myArray.length= 0 ;

     while (myArray.length > 0) {

        myArray.pop();

    }

console.log(“Empty Array Elements: ” + myArray);

    console.log(“Empty Array Length: ” + myArray.length);

}

vacantArray();

Output:

Array Elements: 6,7,8,9,10

Array Length: 5

Empty Array Elements:

Empty Array Length: 0

See Also: How To Convert String To Date In JavaScript In Dd-Mm-Yyyy Format

FAQs

How to check if an array is empty?

You can apply the .length property in JS to verify whether the array is vacant. If the output shows 0, then it is an empty array.

How to create empty Object in JavaScript?

You can make an empty object in JS by incorporating the Object Constructor and curly braces{}.

What is an array in JavaScript?

An array in JS is defined as a kind of global object that stores data.

How do you empty an array into a string in JavaScript?

You can use the toString() function to empty an array into a string in JS.

Conclusion  

In conclusion, we learned how to declare an empty array in JavaScript using the above methods. These methods are simple, and you won’t face any difficulties using them. Additionally, the quickest way to empty an array is to utilize a new empty array. Always remember that the pop method takes a lot of time, and you should use it only when necessary. To improve your skills as a developer, you can learn how to convert an array to an object in JavaScript.  

See Also: What Is The Proper Operator For “Not Equals” In JavaScript?

Leave a Comment