0 votes
in JavaScript by
In JavaScript, how can you empty an Array?

1 Answer

0 votes
by

There are a few methods to empty an array. They are:

  1. Method 1:

arrayList = []

If you do not have any references to the original array arrayList, this method is recommended. However, if you have previously referenced this array from a different variable, then the original reference array will be kept unchanged.

2. Method 2:

arrayList.length = 0;

By using this code, you are setting the array length to 0 thus emptying it of all updates of reference variables, going back to the original array. 

3. Method 3:

arrayList.splice(0, arrayList.length);

This method can also be used to empty all the array including updates of all the references back to the original array.

4. Method 4:

while(arrayList.length)

{

arrayList.pop();

}

This is also a great way to empty arrays but it is not a recommended method.

...