3 Ways to Empty an Array in JavaScript

Опубликовано: 13 Май 2026
на канале: CodeMentorHub
6
1

let arrayList = ['a', 'b', 'c', 'd', 'e', 'f'];

// Method 1: Reassignment
arrayList = [];
// Explanation: By reassigning the array variable to an empty array, you're essentially replacing the original array with a new empty one.

// Method 2: Setting length to 0
arrayList.length = 0;
// Explanation: When you set the length of an array to 0, all elements are removed. This works because an array's length property determines the number of elements in it.

// Method 3: Using splice
arrayList.splice(0, arrayList.length);
// Explanation: The splice method can be used to remove elements from an array. In this case, you're starting at index 0 and removing `arrayList.length` number of elements, effectively emptying the array.

console.log(arrayList); // Output: []