Last modified: Jul 31, 2026
JavaScript Empty an Array: 4 Simple Methods
Clearing an array is a common task in JavaScript. You might need to reset data, clear a list, or free up memory. There are several ways to do this, but not all are equal.
In this guide, you will learn the best methods to empty an array. We will cover simple assignment, the length property, and the splice() method. You will see clear examples and understand when to use each one.
Choosing the right method is important. It affects performance and whether you keep the original array reference. Let’s dive into the details.
Why Empty an Array?
Arrays hold data in your application. Sometimes you need to start fresh. For example, you might clear a shopping cart or reset a list of notifications.
If you just assign a new empty array, you lose the old one. This can be a problem if other parts of your code still point to the original array. The methods below help you manage this effectively.
Remember, the best choice depends on your specific needs. Do you need to keep the same array reference? Or is creating a new array acceptable?
Method 1: Assigning a New Empty Array
The simplest way is to assign a new empty array to your variable. This is quick and easy to write.
However, this method creates a brand new array. If you have another variable referencing the old array, that variable will still hold the old data. This can lead to bugs if you are not careful.
Here is a simple example:
// Create an array
let fruits = ['apple', 'banana', 'orange'];
console.log(fruits); // Output: ["apple", "banana", "orange"]
// Create a reference to the same array
let oldFruits = fruits;
// Empty the array by assigning a new one
fruits = [];
console.log(fruits); // Output: []
console.log(oldFruits); // Output: ["apple", "banana", "orange"]Output:
["apple", "banana", "orange"]
[]
["apple", "banana", "orange"]As you can see, oldFruits still points to the original array. This method is best when you don't need to keep the original reference. It is also very fast.
This is a straightforward approach for most cases. Just be aware of the reference issue.
Method 2: Setting the Length Property
This is one of the most efficient and clean methods. You can set the length property of an array to 0 to empty it. This method modifies the original array in place.
It is very fast because it directly changes the array's length. All other variables pointing to the array will also see the change. This is perfect when you need to keep the same array reference.
Let's see it in action:
// Create an array
let numbers = [1, 2, 3, 4, 5];
console.log(numbers); // Output: [1, 2, 3, 4, 5]
// Create a reference
let oldNumbers = numbers;
// Empty the array by setting length to 0
numbers.length = 0;
console.log(numbers); // Output: []
console.log(oldNumbers); // Output: []Output:
[1, 2, 3, 4, 5]
[]
[]Notice how oldNumbers is also empty now. This is because both variables point to the same array object. This method is very reliable and efficient.
It is often the recommended way to clear an array while keeping its identity. You can also learn more about the length property in our JavaScript Array Length Guide.
Method 3: Using the Splice Method
The splice() method is a powerful tool. It can add or remove items from an array. To empty an array, you can use splice(0, array.length). This removes all elements starting from index 0.
This method also modifies the original array in place. It returns an array containing the removed elements. This can be useful if you need to process the old data before clearing.
Here is how you use it:
// Create an array
let colors = ['red', 'green', 'blue'];
console.log(colors); // Output: ["red", "green", "blue"]
// Create a reference
let oldColors = colors;
// Empty the array using splice
let removedItems = colors.splice(0, colors.length);
console.log(colors); // Output: []
console.log(oldColors); // Output: []
console.log(removedItems); // Output: ["red", "green", "blue"]Output:
["red", "green", "blue"]
[]
[]
["red", "green", "blue"]As you can see, the original array is cleared. The removedItems variable holds the old elements. This is great if you need to keep the old data for some reason.
It is a bit more verbose than the length method but offers flexibility. For a deeper dive into array methods, check out our JavaScript Array Methods Guide.
Method 4: Using Pop in a Loop
This is the least recommended method. It is slow and inefficient. You use a loop to call the pop() method until the array is empty. This method removes the last element of the array.
While it works, it is not a good practice for clearing large arrays. It is much slower than the other methods. You should avoid this unless you have a very specific reason.
Here is an example to show why it's not ideal:
// Create an array
let items = ['a', 'b', 'c', 'd'];
console.log(items); // Output: ["a", "b", "c", "d"]
// Empty the array using a loop with pop
while (items.length > 0) {
items.pop();
}
console.log(items); // Output: []Output:
["a", "b", "c", "d"]
[]This method is clear in its intent but very inefficient. It performs an operation for each element. For large arrays, this will be noticeably slow.
We recommend using the length or splice() methods instead. They are much better for performance.
Performance and Best Practices
When you need to empty an array, performance matters. The length = 0 method is generally the fastest. It is a simple operation that the JavaScript engine can optimize well.
The splice() method is also fast, but it has to create a new array for the removed items. This adds a little overhead. The assignment method is fast but creates a new object.
For most applications, the difference is negligible. However, for large arrays or frequent operations, it can add up. In most cases, setting length to 0 is the best practice.
It is simple, fast, and keeps the original reference. This makes your code predictable and less prone to bugs.
Common Mistakes to Avoid
One common mistake is reassigning the array when you need to keep the reference. As we saw, this can cause issues with other variables. Always think about what your code depends on.
Another mistake is using the loop method for large arrays. It is a waste of resources. Stick to the efficient methods we discussed.
Also, be careful not to confuse pop() with clearing. pop() removes only the last element. You need a loop or another method to remove all elements.
Understanding these pitfalls will save you time and frustration. For more on working with arrays, see our JavaScript Array Variables Guide.
Choosing the Right Method
So, which method should you use? It depends on your situation.
If you want to keep the original array reference, use length = 0 or splice(). If you don't need the reference, you can simply assign a new array. If you need the removed items, use splice().
Here is a quick summary:
- Assignment: Fast, but changes the reference.
- Length: Fastest, keeps reference, simple.
- Splice: Keeps reference, returns removed items.
- Loop: Slow, not recommended.
For most cases, using length = 0 is the recommended approach. It is clean, efficient, and easy to understand.
Conclusion
Emptying an array in JavaScript is a simple task with several solutions. You can assign a new array, set the length property to 0, or use the splice() method. Each has its own use case.
We recommend using the length property for most situations. It is fast and keeps the original array reference. This makes your code more reliable and easier to maintain.
Remember to avoid the loop method for performance reasons. Now you have the knowledge to clear arrays effectively in your projects.
For more advanced topics, you can explore how to convert arrays to strings or work with complex data. Check out our guide on JavaScript Array to String or JavaScript Array of Objects Guide to expand your skills.