2

I have an array eg: a[3,4,5,6,7,8]. I want to remove all the element in one time and make array as an empty array. How to remove all the element of an array.

My code

var a = [2,3,4,5,6];
for(var i=0; I<a.length; i++){
a.remove();
} 
David
  • 3,817
  • 8
  • 26
  • 54

2 Answers2

4
a.length = 0;

This is all what you need

var a = [2,3,4,5,6];
console.log(a);
a.length = 0;
console.log(a);
Adam Azad
  • 10,915
  • 5
  • 26
  • 67
2

Do a.length = 0; if you don't want to lose references. Do a = []; if you want to lose references.

Klaider
  • 3,565
  • 3
  • 23
  • 52