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 How to's
Edit on Github

How to use the Javascript Array Clear method?

Written by:

One of the most common methods to clean arrays in JS is to assign an empty array to our variable. Let's take a look at it.

1let colors = ["yellow", "green", "blue", "red", "black"]; 2colors = [];

This approach is fast, easy to understand, and works in all Javascript environments. However, it creates a new array object and discards the old one, which can have performance implications if you're working with large arrays or need to maintain references to the original array.

In addition to allocating a new empty array, there are other methods available to solve this requirement. However, not all of them offer the same level of efficiency. Here are a few methods along with some data to help you make an informed decision.

Methods to Clear a Javascript Array

1. Set the length of the array to zero.

Another common approach to clearing an array in Javascript is to set its length to zero using the array.length property. This effectively removes all elements from the array.

1let animals = ["dog", "cat", "bird", "horse", "cow"]; 2animals.length = 0;

This method is also fast, easy to understand, and works in all JavaScript environments. However, it can have unexpected side effects if you're not careful. For example, if there are any other references to the original array, they will not be updated by this method and may still hold references to the original array's values.

2. Using the splice() method

The splice() is a javascript method that gives us the ability to add or delete elements from an array. To clean an array, use splice() with a beginning index of zero and a deleted number equal to the array's length.

1let animals = ["dog", "cat", "bird", "horse", "cow"]; 2animals.splice(0, animals.length);

This method is also fast and generally supported, and it can be useful if you need to remove only a particular element of the array instead of clearing it entirely. However, if you're working with really large arrays, it may have some performance concerns because it needs iterating through every element in the array.

3. Using a while loop

You can clear an array by iterating over it using a while loop and removing each element from the array one at a time with the pop() method.

1let animals = ["dog", "cat", "bird", "horse", "cow"]; 2while (animals.length) { 3 animals.pop(); 4}

This method is flexible and can be beneficial if additional processing or filtering is required on the elements as they are removed. However, it can be slower than some of the other ways, especially for large arrays.

Utilizing benchmarking to figure out which option offers the best performance is an interesting idea. Here is an example of the many clearing techniques that have been evaluated using a benchmarking tool.

Regardless of which method you choose, there are a few best practices. All of them have different behaviors that can generate different repercussions in our code. Take into account that your code must be understood by other developers in the future, so you should always try to choose the cleanest and easiest-to-understand option. You can learn more about methods of arrays in Javascript or for example the Javascript array some method at 4Geeks Blog.