Array.prototype.length = 0; // This sets the array’s length property to 0
Checkout this video:
What is an Array?
In order to clear an array, there are two approaches that can be taken. The first is to simply set the length property of the array to 0:
array.length = 0;
The second approach is to use the built-in splice() method:
Why would you need to clear an Array?
-To release memory that is no longer needed (for example, if you’re finished using the Array and don’t plan to use it again)
-To empty an Array so it can be reused (for example, if you want to store a new set of data in the same Array)
The most common way to clear an Array is to set its length property to 0. This will remove all elements from the Array and will leave it empty:
myArray.length = 0;
Another way to clear an Array is to use the splice() method. The splice() method can be used to remove elements from an Array, but it can also be used to add new elements. To empty an Array using the splice() method, you can pass 0 as the second argument (the number of elements to remove):