-3

I have an array like this:

var animals=["cat","dog","snake","rhino"];

But sometimes I have to delete this array(remove it from the dom). I have tried animals.remove; and $(animals).remove() and animals.remove() but none of them did the trick.Any ideas?

stranger4js
  • 269
  • 4
  • 15

3 Answers3

3
var animals=["cat","dog","snake","rhino"];

then to clear it do:

animals=[];

or

animals.length=0;

or

while(animals.length > 0) {
   animals.pop();
}
Suchit kumar
  • 11,809
  • 3
  • 22
  • 44
2

Just Clear The Array

Using This 2 Methods

  1. animals.length =0
  2. animals=[];
T J
  • 42,762
  • 13
  • 83
  • 138
Dhaval
  • 2,341
  • 1
  • 13
  • 16
2

Just assign the animals array to a value undefined and the array data will be dereferenced and garbage collected.

Donot try to call delete operator that is explicit removal.

animals = undefined OR

animals = void 0

Thanks

Kiba
  • 10,155
  • 6
  • 27
  • 31