6.7 Empty an Array

What if you want to remove all elements from an array? There are four common solutions, each with its own trade-offs depending on whether other variables reference the same array.

Four solutions

  • 1. Reassign: numbers = []; — only works if the variable was declared with let. Other references to the original array still see the old elements.
  • 2. Set length to 0: numbers.length = 0; — truncates the array in place. All references see it become empty.
  • 3. splice: numbers.splice(0, numbers.length); — removes every element from the start. Same effect as setting length to 0.
  • 4. pop in a loop: while (numbers.length > 0) numbers.pop(); — works but is slow on large arrays.
const numbers = [1, 2, 3, 4];
const another = numbers;

// Solution 1 — only good if no other references
// numbers = [];   // requires let, not const

// Solution 2 — recommended
numbers.length = 0;

console.log(another); // []  -- shares the same array, so it's also empty

Solution 1 looks tempting, but it has a subtle drawback: any other variable still pointing at the original array (here another) keeps seeing the old elements, because numbers simply moves to a brand-new array. Solutions 2 and 3 mutate the array in place, so every reference is affected. Solution 4 is technically correct but bad for performance on a one-million-element array.

In practice, prefer setting length to 0 or using splice — they are short, clear and work no matter how many references point at the array. See you in the next video.

Summary

This lesson teaches four methods to empty an array in JavaScript: reassigning to a new empty array (only works without other references), setting the length property to 0, using the splice() method, and looping with pop(). The instructor emphasizes that setting length to 0 is the most reliable and performant solution, while the pop() loop should be avoided due to poor performance on large arrays.

Key points

  • Reassigning an array declared with `const` is impossible—use `let` if you need to reassign to a new empty array
  • Setting `length = 0` empties an array and works regardless of other references to the original object
  • The `splice()` method can empty an array by starting at index 0 and removing all remaining elements
  • Looping with `pop()` works but has poor performance on large arrays and is not recommended
  • If you reassign with a new array and other variables reference the original, that original array persists in memory until garbage collected

FAQ

Why can't I reassign an array declared with const?

The `const` keyword prevents reassignment of the variable itself. If you need to reassign the array, you must declare it with `let` instead.

Which method should I use if my array has multiple references from other variables?

Use `length = 0` or `splice()`, as these methods mutate the original array object. Reassignment creates a new array and only updates the current variable, leaving other references pointing to the old array.

What's the best practice for emptying an array in JavaScript?

Setting `length = 0` is the recommended approach—it's simple, efficient, works with multiple references, and performs well even on large arrays.