4Geeks chosen to deliver AI education in the Bahamas alongside Harvard, Oxford, and Columbia.See more

How to use the Javascript Array Clear method?

By 4Geeks

Empty an array with length = 0 or splice(), understand why arr = [] behaves differently and clear arrays in React state.

5 min readLast updated
Clear an array in JavaScript icon

How do you clear an array in JavaScript?

Short answer: set its length to zero with arr.length = 0. It empties the array itself, works with const and every variable that points to that array sees it empty. If you only need a fresh empty array in one variable, arr = [] also works, but the old array keeps its items.

javascript
const fruits = ["apple", "banana", "cherry"];
fruits.length = 0;
console.log(fruits);   // []

There is no built-in clear() method for arrays. Set and Map have one, arrays don't.

length = 0 empties the array everywhere, = [] only gives one variable a new array, and a const can't be reassigned.

Every way to clear an array

MethodCodeChanges the original arrayWorks with constReturns
Set the length to 0arr.length = 0YesYes0
splice()arr.splice(0)YesYesThe removed items
Assign a new arrayarr = []No, other references keep the itemsNo[]
pop() in a loopwhile (arr.length) arr.pop()YesYesNothing, and it's slow

For most code, arr.length = 0 is the clearest choice. Use splice(0) when you also want the items you removed.

Clear an array with length = 0

An array's length can be written. Setting it to 0 removes every item, and setting it to a smaller number keeps only the first ones:

javascript
const nums = [1, 2, 3, 4, 5];
 
nums.length = 2;
console.log(nums);   // [1, 2]
 
nums.length = 0;
console.log(nums);   // []

Because it changes the array itself, it also empties arrays inside objects, like cart.items.length = 0.

Career · AI Engineering for Developers

You already handle arrays and state, now build AI apps with them

Use the coding skills you already have to build AI agents, RAG systems and LLM-powered apps, with mentors and real projects.

Clear an array with splice()

splice(0) removes every item from position 0 and returns them in a new array. Use it when you want to empty the array and keep what was in it:

javascript
const queue = ["task1", "task2", "task3"];
const done = queue.splice(0);
 
console.log(queue);   // []
console.log(done);    // ["task1", "task2", "task3"]

splice(2) removes everything from position 2 onwards, so [1, 2, 3, 4, 5].splice(2) leaves [1, 2].

arr = []: a new array, not an empty one

Assigning [] doesn't empty the array. It makes the variable point to a new empty array. Any other variable, function or object that had the old array still sees its items:

javascript
let current = [1, 2, 3];
const saved = current;
 
current = [];
console.log(current);   // []
console.log(saved);     // [1, 2, 3]

Compare it with length = 0:

javascript
const current = [1, 2, 3];
const saved = current;
 
current.length = 0;
console.log(saved);     // []

arr = [] is fine when nothing else uses the old array, for example a local variable you are about to fill again. It needs let: with const you get TypeError: Assignment to constant variable.

Clear an array in React and other state

In React state, don't change the array in place. Set a new empty array instead, so React sees the change:

javascript
const [items, setItems] = useState(["a", "b"]);
 
setItems([]);   // correct
// items.length = 0;   // the screen doesn't update

The same applies to Vue's ref([]) with list.value = [], and to any store that compares references to detect changes.

Check that an array is empty

javascript
const arr = [];
 
if (arr.length === 0) {
  console.log("empty");
}
 
Array.isArray(arr) && arr.length === 0;   // true

Don't compare with arr === []: that is always false, because it compares two different arrays.

Use AI to clear arrays the right way

An assistant like ChatGPT, Claude or GitHub Copilot can tell you which method fits your code and why an array isn't emptying. These three prompts are ready to copy.

1. Choose a method

In my JavaScript code I need to empty this array: [paste your code].
Other parts of the code use it like this: [explain or paste].
Which method should I use and why?

2. Explain a bug

I clear an array in JavaScript with [your code], but [what happens] and I expected
[what you expected]. Explain why and how to fix it.

3. Clear state in a framework

I use [React / Vue / Angular] and I want to empty this list in my state: [paste the code].
Show me the correct way so the screen updates.

Test the answer with console.log() on every variable that points to the array.

Learn with a mentor

From array methods to shipping AI features

In the 4Geeks AI Engineering for Developers program you add AI agents, RAG and production LLM apps to your portfolio.

Common problems when clearing an array

What you seeWhy it happensFix
Another variable still has the itemsarr = [] creates a new arrayUse arr.length = 0
TypeError: Assignment to constant variable.You wrote arr = [] on a constUse arr.length = 0, or declare it with let
arr.clear is not a functionArrays have no clear() methodarr.length = 0 (clear() exists on Set and Map)
The React screen doesn't updatelength = 0 changes the array without telling ReactsetItems([])
TypeError: Cannot assign to read only property 'length'The array is frozenCreate a new array instead of changing it
arr === [] is always falseIt compares two different arraysarr.length === 0
Clearing a big array with pop() is slowIt removes items one by onearr.length = 0
Free download

Clearing arrays in JavaScript in a 2-page cheat sheet

length = 0, splice(), arr = [], const, React state, checking for empty arrays, common errors and prompts for ChatGPT, Claude or Copilot. Free, in PDF.

How to clear an array in JavaScript cheat sheet

2 pages · PDF · 4Geeks

  • Every way to clear an array, side by side
  • When arr = [] leaves the old items behind
  • Common errors and prompts to solve them with AI

Get it free

Your PDF right away

Frequently asked questions about clearing arrays in JavaScript

Want to go from array methods to building AI apps, with real projects and mentors?

See the AI Engineering for Developers program