To empty an array in Javascript you can use the following methods.

Sample Javascript

myArray = [];

This code will set the variable A to a new empty array (take care if you have references to the “old” array).

or

myArray.length = 0

This will clear the existing array by setting its length to 0.

or

myArray.splice(0,myArray.length)

Working, but not recommended for this task. see Array.prototype.splice()

or

while(myArray.length > 0) {
    myArray.pop();
}

Fastest solution, without breaking references.

7 thought on “How to empty an array in Javascript”

Leave a Reply