How to Empty a JavaScript Array

There are several ways to empty a JavaScript array, but they are not equivalent. Use array.length = 0 when you want to clear the existing array object; use array = [] when you only need to reassign that variable to a new empty array.

const scripts = ["PHP", "JavaScript", "Python"];
scripts.length = 0;
console.log(scripts); // []

Empty the Existing Array with length = 0 Top ↑

const scripts = ["PHP", "ASP", "JavaScript", "HTML"];
scripts.length = 0;
console.log(scripts); // []

This mutates the array object itself, so all references to that same array see it become empty.

Reassign a Variable to a New Empty Array Top ↑

let scripts = ["PHP", "JavaScript", "Python"];
scripts = [];
console.log(scripts); // []

This does not clear the old array object. It simply makes the variable refer to a new array.

Why Other References Change the Result Top ↑

let scripts = ["PHP", "JavaScript"];
const alias = scripts;

scripts = [];
console.log(scripts); // []
console.log(alias);   // ["PHP", "JavaScript"]

Compare with length = 0:

const scripts = ["PHP", "JavaScript"];
const alias = scripts;

scripts.length = 0;
console.log(alias); // []

This distinction is the most important reason to choose one emptying technique over another.

Clear an Array with splice() Top ↑

const scripts = ["PHP", "JavaScript", "Python"];
const removed = scripts.splice(0);

console.log(scripts); // []
console.log(removed); // ["PHP", "JavaScript", "Python"]

splice(0) mutates the existing array and also returns an array containing the removed elements. Use it when those removed values are useful; otherwise length = 0 is simpler.

Remove Every Element with a pop() Loop Top ↑

const scripts = ["PHP", "JavaScript", "Python"];

while (scripts.length > 0) {
  const removed = scripts.pop();
  console.log("Removed:", removed);
}

This is appropriate only when you need to process each removed value. It is unnecessarily verbose if the goal is simply to clear the array.

Can You Empty an Array Declared with const? Top ↑

Yes. const prevents reassignment of the variable, but the array object can still be mutated.

const scripts = ["PHP", "JavaScript"];
scripts.length = 0; // allowed

// scripts = [];    // TypeError: assignment to constant variable

Which Array-Emptying Method Should You Use? Top ↑

MethodMutates same array?Other references see empty array?Best use
array.length = 0YesYesClear existing array object simply
array = []No; reassigns variableNoDiscard this variable's reference
array.splice(0)YesYesClear and capture removed elements
Repeated pop()YesYesProcess values while removing them

Common Array-Emptying Mistakes Top ↑

  • Assuming array = [] empties the object referenced elsewhere.
  • Trying to reassign a const variable to [].
  • Using delete array[index] to clear items; delete can leave empty slots and does not reduce length.
  • Using a loop when length = 0 directly expresses the goal.

Array length Property Remove from End with pop() Remove by Position with splice()




Subscribe to our YouTube Channel here



plus2net.com










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer