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
  • 4,266
  • 8
  • 34
  • 69

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
  • 11,171
  • 5
  • 29
  • 70
2

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