
How to Empty an Array in JavaScript?
There are a number of ways to empty an array in JavaScript, probably the simplest is to reassign the variable to an empty array:
Copied to clipboard!
let array = [1, 2, 3];
// Assigning to an empty array
array = [];
You can also set the length of the array to 0:
Copied to clipboard!
// Setting its length property to 0
array.length = 0;
Splice can be also used get rid of everything from an array:
Copied to clipboard!
// Using splice with the array's length
array.splice(0, array.length);
So which one to use? The simplest, and also the fastest operation is simply reassigning the variable to an empty array. To see the performance difference, checkout the 3 solutions on JSBen.

Resources:
π More Webtips
Master the Art of Frontend
Access exclusive interactive lessons
Unlimited access to hundreds of tutorials
Remove ads to learn without distractions