length property to
const array = [1, 2, 3]; array.length = 0; console.log(array); // Logs ""
The above solution is actually not the fastest. The below solution is the most performant:
let array = [1, 2, 3]; array = ; console.log(array); // Logs ""
However, there are two reasons why I don’t use the above solution and instead set the length to
If the array is declared using
const, then setting the array to
will not work. It must be declared using
If “Array B” references “Array A”, and Array A is emptied by being set to
, then Array B will not be automatically emptied as well.