By Henri Parviainen
We can clear an array by assigning a new empty array as its value.
This method works as long as you don't have any references pointing to the original array.
See the example code below.
Use this method when you want to clear the original array but also want to keep all the references unchanged or if you don't have any references.
Another method we can use to clear an array is to set its length property to zero. If we set array length to zero, all elements of the array will be deleted. This includes all reference arrays as well.
languages.length = 0; console.log(languages); // Prints [ ] console.log(courses); // Prints [ ]
Use this method when you want to clear both the original array and all references it may have. This is the most recommended and fastest way to remove all array elements from the whole array.
We can also empty all values from array with a
splice array method.
languages.splice(0, languages.length); console.log(languages); // Prints 
The first parameter we give to
splice is the index where we start. In the second parameter, we define how many indexes will be removed.
Splice works just like method 2 so you can use it if you want to clear the array content as well as all references.