Self-paced

Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.

Bootcamp

Learn live

Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.

Upcoming live events

Learning library

For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.

It makes sense to start learning by reading and watching videos about fundamentals and how things work.

Search from all Lessons


LoginGet Started
← Back to Lessons
Edit on Github

All you need to know about Javascript Empty Array

Empty Javascript Array
How to empty an array in Javacript?

Empty Javascript Array

1// Example Array 2let n = [ 1, 2, 3, "a", "b", "c" ]; 3 4// Check if an array is empty in Javascript 5if (Array.isArray(n) && n.length === 0) { 6 console.log(“The array n is empty”); 7 8} else { 9 console.log(“The array n is not empty”); 10}; 11 12// Empty an array in Javascript by rewriting the length property 13n.length = 0; 14console.log(n) // output: []

There are occasions in Javascript in which we need to empty an array or check if the array is empty, so we can work with it. We have several ways to achieve this goal, we will see some of them next. If you want to know more about What is an array in Javascript you can read our guide on the 4Geeks Blog.

How to check if an Array is empty?

The best way to check if an array is empty is by accessing the length property of the array.

1let n = []; 2 3if ( Array.isArray(n) && n.length === 0 ) { 4 console.log(“The array n is empty”); 5} else { 6 console.log(“The array n is not empty”); 7};

In this example, we first verify that the array is of type array with the expression Array.isArray(n), because if it was another type of data, for example, an object we couldn't access the length property, then we verify that the array is empty with the expression n.length === 0.

How to empty an array in Javacript?

There are different ways in which we can empty an array in Javascript. Next, we will see some of them.

Rewrite the property length

1let n = [ 1, 2, 3, 4, 5 ]; 2n.length = 0 3 4console.log( n ) // []

The most efficient way in terms of execution when emptying an array in Javascript is by accessing the length property of the array and assigning the value 0, this removes all the elements of the array leaving it completely empty.

Assign it a new empty array

Another way to empty an array in Javascript is by assigning it a new empty array.

1let n = [1, 2, 3]; 2const m = ["a", "b", "c"]; 3 4n = []; 5m = []; 6 7console.log(n) // (output) [] 8console.log(m) // (output) TypeError: Assignment to constant variable.

In this example, we assigned a new empty array [] to the n array, removing all its elements.

It is important to have in mind that if you use the const keyword to create the array, this syntax won't work for emptying an array because the constants can't be modified as in the array example m.

Using the splice() method

We can also use the splice() method to empty an array in Javascript.

1let n = [1, 2, 3]; 2let m = ["a", "b", "c"]; 3 4n.splice(0, n.length); 5m.splice(0, 1); 6 7console.log(n); // (output) [] 8console.log(m); // (output) ["b", "c"]

The splice method allows you to remove the elements of an array from a specific index, this method receives 2 numeric parameters, the first is the index from which you want to start removing elements, the second parameter is the number of elements you want to remove.

Using the pop() method

Finally, another option to empty an array is by using the pop() method.

1let n = [1, 2, 3, 4, 5]; 2 3while (n.length > 0) { 4 n.pop(); 5}; 6 7console.log(n) // (output) [];

This is another way in which you can empty an array in Javascript. Using the pop method and the while() loop removes all the elements of the array one by one.

In all the previous examples the elements are removed from the array, but not from the memory space, these elements are still in local memory, but since they don't have any reference the Javascript garbage collection ends up removing them. It is important to have this in mind because having elements in the memory without references could slow down the execution of your code.