In this tutorial, you’ll learn about three ways to empty an Array in JavaScript.

Empty An Array Using Splice

Splice method modifies the content of an array by removing or replacing the existing elements. You can call splice method with 3 parameters: start index from where to modify the array, number of items to delete and the items to add. To empty an array using splice you only need to provide the start index from where to modify the array which is 0. The second argument (number of items to delete) if not provided is assumed to be the length of the array.

let fruits = ["Orange", "Apple", "Pineapple"];
console.log(`Length of array before splice ${fruits.length}`)''
fruits.splice(0);
console.log(`Length of array after splice ${fruits.length}`);

Empty An Array By Setting the Length as 0

Another method to empty an array in JavaScript is to set the length of the array as zero.

let fruits = ["Orange", "Apple", "Pineapple"];
fruits.length = 0;

Empty An Array By Assigning a New Array

Assigning a new array to an existing array also make the array empty.

let fruits = ["Orange", "Apple", "Pineapple"];
fruits = [];