javascript tutorial - [Solved-5 Solutions] Empty an array - javascript - java script - javascript array
Problem:
Is there a way to empty an array and if so possibly with .remove()?
Solution 1:
If we need to keep the original array because we have other references to it that should be updated too, we can clear it without creating a new array by setting its length to zero:
Solution 2:
A more cross-browser friendly and more optimal solution will be to use the splice
method to empty the content of the array A as below:
A.splice(0, A.length)
;
Solution 3:
Performance test:
Solution 4:
We can add this to your JavaScript file to allow your arrays to be "cleared":
Then we can use it like this:
Or if we want to be sure we don't destroy something: