4 Ways to Empty an Array in JavaScript

Introduction
How do you empty an array in JavaScript?
You can empty an array by using one of the four methods listed below:
  1. Create a new empty array in the original array's place
  2. Set the length of the array to a value of 0
  3. Use the splice() function to remove each item from the 0 to array.length index position in the array
  4. Loop over the array and pop() each item until the array is empty
In this article, we'll go over each of those methods and provide you with some coding examples as well.
Let's get started!
Table of Contents
  1.  Create a New Empty Array
  2.  Set Length To Zero
  3.  Using Splice()
  4.  Loop Over & Remove Each Value
1. Create a New Empty Array
The first method we'll cover is to set an array as empty by simply giving it a new value of [].
If you plan on using this method, make sure you don't have other references to the original array in your code. If you have other references to the original array, those references to the original array will remain the same.
Here's what the code would look like for this method:
JavaScript
 
let names = ["Johnny", "Billy", "Sandy"]
 
names = []
 
// Output: []
This method is the fastest compared to the other ones we'll show you in this article.
For demonstration purposes, here's a code example that shows the issue you can encounter if you use this method when having multiple references to the original array in your code:
JavaScript
 
let names = ["Johnny", "Billy", "Sandy"]
 
let namesVersion2 = names
 
names = []
 
console.log(namesVersion2)
 
// Output: ["Johnny", "Billy", "Sandy"]
Notice how the second version of the array was not changed and was not emptied. This is something to be aware of when using this in your code.
2. Set Length To Zero
The second method we'll cover uses the .length method to set the length of the original array to a value of 0.
This is the second-fastest method of the four covered in this article. And this method will work with all the different JavaScript implementations.
Here's what the code looks like:
JavaScript
 
const names = ["Johnny", "Billy", "Sandy"]
 
names.length = 0
 
// Output: []
Simple enough, right?
3. Using Splice()
The splice() function is used to modify arrays by removing or replacing existing items or adding new ones in their place.
The splice() function can be used to completely empty a given array if it's given a certain set of arguments.
Here's what the code looks like:
JavaScript
 
const names = ["Johnny", "Billy", "Sandy"]
 
names.splice(0, names.length)
 
// Output: []
The first argument tells splice() the index at which it should start modifying the array. We give that argument a value of 0, which tells splice() to start at the beginning.
And the second argument tells splice() how many items from the beginning you want to delete from the array. By giving that argument a value of names.length, we tell it to remove all the items in the array.
When you console.log() the newly modified array, you'll find it to be empty.
It's also worth noting that the splice() function will return an array containing the deleted elements.
You can check the browser compatibility at this link.
4. Loop Over & Remove Each Value
The fourth and last method we'll cover is using a loop to pop() each item in the array one-by-one.
The pop() function removes the last item from an array and returns the value of the item that was removed.
We'll be using the while statement as our looping mechanism, but you can use any other of the several types of loops and get the same result.
Here's what the code looks like in practice:
JavaScript
 
const names = ["Johnny", "Billy", "Sandy"]
 
while (names.length > 0) {
  names.pop()
}
 
// Output: []
This method is the slowest method of the four and is the least succinct in terms of the code syntax and structure.
If you logged the original array, you'd notice that it is now empty.
The pop() function is widely supported by browsers.
Conclusion
There you have it!
To recap, we covered four different ways to empty an array in JavaScript:
  1. Create a new empty array in the original array's place
  2. Set the length of the array to a value of 0
  3. Use the splice() function to remove each item from the 0 to array.length index position in the array
  4. Loop over the array and pop() each item until the array is empty
Thanks for reading and happy coding!