views:

84

answers:

7
var arr = [-3, -34, 1, 32, -100];

How can I remove all items and just leave an empty array?

And is it a good idea to use this?

arr = [];

Thank you very much!

A: 

one of those two:

var a = Array();
var a = [];
Johannes Weiß
arr = [] is recommended as it takes up less space (code wise).
Zoidberg
And it is sexier.
Stephen
@Stephen: `[]` turns you on?
BoltClock
literally. ::drum-roll rim-shot::
Stephen
+1  A: 

Just as you say:

arr = [];
BoltClock
A: 

Using arr = []; to empty the array is far more efficient than doing something like looping and unsetting each key, or unsetting and then recreating the object.

Stephen
A: 

While you can set it to a new array like some of the other answers, I prefer to use the clear() method as such:

array.clear();
GotDibbs
+6  A: 
John Kugelman
This is the answer. I wrote pretty much the same before spotting that John had already done it.
Tim Down
A: 

The following article should answer part 2. http://stackoverflow.com/questions/864516/what-is-javascript-garbage-collection

Thomas Langston
A: 

Out of box idea:

while(arr.length) arr.pop();
M28
Will work, but is needlessly inefficient. `arr.length = 0` will be quicker and easier to read.
Tim Down