To clear a JavaScript array, set its length to 0, use splice(), or assign a new empty array. The right method depends on whether you need to empty the existing array for every reference to it, or simply make one variable point to a different array. Understanding that distinction helps you avoid bugs when arrays are shared between functions, objects, or parts of an application.
In this guide, you’ll learn how to clear a JavaScript array, when to use each approach, and what to watch out for with const, nested arrays, and application state.
Quick Answer: How Do I Clear a JavaScript Array?
Use array.length = 0 when you want to empty an array in place while keeping the same array object:
const colors = ["red", "green", "blue"];
colors.length = 0;
console.log(colors); // []
Alternatively, use array.splice(0) to remove all items in place, or assign [] to a variable when you want it to refer to a new array. These options can look similar, but they behave differently when other code holds a reference to the original array.
Method 1: Set the Array’s Length to Zero
Setting an array’s length property to 0 is a concise way to clear it:
let tasks = ["email client", "write report", "review code"];
tasks.length = 0;
console.log(tasks); // []
JavaScript arrays have a writable length property. When you set that property to a smaller number, JavaScript removes elements at indexes equal to or greater than the new length. Setting it to zero removes every element.
This method changes the existing array rather than creating a replacement. That matters when another variable or object property refers to the same array:
const firstReference = ["apples", "oranges"];
const secondReference = firstReference;
firstReference.length = 0;
console.log(secondReference); // []
Both variables point to the same array object, so clearing it through either reference makes the empty array visible through both. Use this method when other parts of your code need to keep their reference to the original array.
Method 2: Use Splice to Remove Every Element
The splice() method can remove elements from an array in place. Calling it with a starting index of zero removes all elements from the beginning to the end:
const scores = [88, 92, 76, 100];
scores.splice(0);
console.log(scores); // []
Because splice() mutates the original array, other references to that array will also see it become empty. The method returns an array containing the removed elements, which can be useful if you need to save or inspect them:
const messages = ["Hello", "How are you?", "Goodbye"];
const removedMessages = messages.splice(0);
console.log(messages); // []
console.log(removedMessages); // ["Hello", "How are you?", "Goodbye"]
Be precise with the arguments. Calling splice(0) removes all elements from index zero onward, but calling splice() with no arguments does not remove anything. If you only need to clear an array and do not need the removed values, array.length = 0 is often shorter and easier to recognize.
Method 3: Assign a New Empty Array
You can make a variable refer to a fresh empty array by assigning it []:
let queue = ["job 1", "job 2", "job 3"];
queue = [];
console.log(queue); // []
This does not empty the original array. It changes what the queue variable refers to. Any other variable that still points to the old array will continue to see its contents:
let queue = ["job 1", "job 2"];
const savedQueue = queue;
queue = [];
console.log(queue); // []
console.log(savedQueue); // ["job 1", "job 2"]
Assigning a new array is a good choice when you own the variable and do not need to preserve the original array’s identity. It is also a common pattern in code that favors creating new values instead of mutating existing ones.
Does It Matter Whether the Array Uses const or let?
Yes. A variable declared with const cannot be assigned a different value, so this will cause an error:
const items = [1, 2, 3];
items = []; // TypeError: Assignment to constant variable
However, const prevents reassignment of the variable; it does not make the array itself immutable. You can still change the contents of a const array:
const items = [1, 2, 3];
items.length = 0;
console.log(items); // []
You can also use splice() with a const array. If you want to replace a const variable’s array with a new one, you cannot reassign that variable. Instead, declare it with let, or update the existing array in place.
Which Array-Clearing Method Should You Choose?
-
Use
array.length = 0when you want a short, direct way to empty the original array. -
Use
array.splice(0)when you want to empty the original array and possibly keep the removed elements. -
Use
array = []when you want a variable to point to a new array and do not need other references to update.
For example, if a function receives an array and is expected to empty the caller’s array, mutate it with length = 0 or splice():
function clearItems(items) {
items.length = 0;
}
const shoppingList = ["bread", "milk"];
clearItems(shoppingList);
console.log(shoppingList); // []
If you instead assign items = [] inside the function, you only change the local parameter. The caller’s variable will still point to the original array:
function replaceItems(items) {
items = [];
}
const shoppingList = ["bread", "milk"];
replaceItems(shoppingList);
console.log(shoppingList); // ["bread", "milk"]
Why Array References Matter
JavaScript arrays are objects. When you assign an array to another variable, JavaScript copies the reference to that array, not the array’s contents. As a result, two variables can refer to the same underlying array. Mutating that array changes what both variables see, while reassigning one variable does not change the other.
const original = ["a", "b"];
const alias = original;
original.splice(0);
console.log(alias); // []
const replacement = ["x", "y"];
const anotherAlias = replacement;
replacement = []; // Error: replacement was declared with const
In the last example, the reassignment error occurs because replacement is declared with const. If it were declared with let, assigning [] would make only replacement refer to the new array; anotherAlias would still refer to the original one.
Clearing an Array Stored in an Object
The same choice applies when an array is stored as an object property. Mutating the property’s array empties it for every reference to that array:
const cart = {
products: ["book", "pen"]
};
cart.products.length = 0;
console.log(cart.products); // []
Replacing the property gives it a new array instead:
cart.products = [];
Use mutation if other code relies on the original array object. Replace the property if you want the object to hold a fresh array and no longer need the old reference.
What About Nested Arrays?
Clearing an array only removes its top-level elements. It does not reach into nested arrays and clear them individually. For example:
const groups = [["red", "blue"], ["green"]];
groups.length = 0;
console.log(groups); // []
The outer groups array is now empty. If another variable refers to one of the inner arrays, that inner array still contains its values:
const firstGroup = ["red", "blue"];
const groups = [firstGroup];
groups.length = 0;
console.log(firstGroup); // ["red", "blue"]
If your goal is to empty every nested array as well as the outer array, you must clear each inner array explicitly before clearing the outer one:
for (const group of groups) {
group.length = 0;
}
groups.length = 0;
Does Clearing an Array Free Its Memory?
Clearing an array removes its references to its elements, but it does not guarantee that memory is released immediately. JavaScript uses automatic garbage collection. An object can generally be collected when nothing in your program can reach it, and the exact timing depends on the JavaScript engine.
If another variable still refers to the array or one of its objects, those values remain reachable. In most everyday code, you do not need to manage memory manually. Clear or replace arrays when it makes sense for your program’s logic, not as a promise that memory will instantly be returned to the system.
Common Mistakes When Clearing JavaScript Arrays
-
Reassigning when you need to mutate: Setting a local variable to
[]does not clear an array held by another reference. -
Trying to reassign a const variable: Use an in-place method such as
length = 0when the variable is declared withconst. -
Calling
splice()without arguments: Usesplice(0)to remove all elements. - Expecting nested arrays to be emptied automatically: Clearing an outer array does not clear inner arrays that are referenced elsewhere.
- Assuming memory is released immediately: Garbage collection is automatic, and clearing an array does not control exactly when it runs.
Frequently Asked Questions
What is the fastest way to clear an array in JavaScript? For most applications, array.length = 0 is a simple and effective choice when you want to clear the existing array. Performance differences are usually less important than choosing the behavior your code needs.
Can I clear an array without changing its reference? Yes. Set array.length = 0 or call array.splice(0). Both mutate the existing array, so other references to it see the change.
Does array = [] clear the original array? No. It makes that variable point to a new empty array. Other references to the original array remain unchanged.
Can I clear an array declared with const? Yes. You can mutate its contents with array.length = 0 or array.splice(0). You cannot assign a new array to the same const variable.
Conclusion
To clear a JavaScript array in place, set its length to 0 or use splice(0). Both methods empty the existing array, so every reference to it sees the change. To make a variable refer to a new empty array, assign [] instead—but remember that other references to the original array will keep their contents.
Choose the approach that matches your goal: preserve the array’s identity when shared code depends on it, or replace it when you simply need a fresh array. With that distinction in mind, clearing a JavaScript array is straightforward and predictable.
Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.