How do you empty an array? let's see how to clear it and empty all its elements
How do you empty an array?
There are a couple of ways to clean or empty an array in Javascript
The first one and easiest is to set its length
to 0
const arr = [1, 2, 3, 4, 5];
arr.length = 0;
console.log(arr);
// expected output: []
The other way to empty an array is to mutate the original array reference, assigning an empty array to the original variable, for this reason it’s required to use let
instead of const
let arr = [1, 2, 3, 4, 5];
arr = [];
console.log(arr);
// expected output: []