Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

If there is an array, is it possible to empty with .remove()?

For instance,

A = [1,2,3,4];

How can I empty that?

share|improve this question
37  
Please accept a different answer, the accepted answer is just wrong – Juan Mendes Nov 5 '12 at 20:44
1  
I agree - I have typically used the A = [] method before finding this thread, but I almost continued doing my normal thing until I saw @Daniel's highly rated comment on the currently accepted answer – phatskat Jan 19 at 21:01

7 Answers

up vote 132 down vote accepted

Very simple:

A = [];

EDIT

A little extra explanation is required here. The code above A = [] will set the variable A to a new empty array. This is perfect if you don't have references to the original array A anywhere else because this actually creates a brand new (empty) array.

However (as other have pointed out below) if you have references to this array, you can empty the original array by setting its length to 0 or by calling .splice() on the array.

Setting the length to zero is the most efficient solution, but this could create problems in some implementations of Javascript (although I don't know of any).

Using .splice() as in A.splice(0,A.length) will work, but it's not very efficient because the .splice() function will return an array with all the removed items, so it will return a copy of the original array in some (most?) Javascript implementations.

So the preferred way to clear an existing array is:

 A.length = 0;

(Matthew Crumley's answer to this question is probably the best one)

share|improve this answer
304  
this will NOT empty the array, but create a new, empty array. This might cause problems, especially if there are other references to the array. OP: Please consider to accept Matthew's answer instead. It is the cleaner and formally correct approach. – Daniel Baulig Jan 19 '11 at 13:08
14  
To all commenters and downvoters: you are all right, and the answer below (setting the length to 0) is a better answer, but in many cases the solution I presented works perfectly and is more succinct – Philippe Leybaert Oct 14 '11 at 19:12
10  
34 upvotes and 17 downvotes. Doesn't that deserve a special badge? :-) – Philippe Leybaert Jan 17 '12 at 15:50
13  
-1: There is a significant difference between emptying an array and creating a new one (as stated by @DanielBaulig). – David Faivre Aug 19 '12 at 23:16
3  
This does not clear the array. Simply replaces it with a blank one. The problems with this is that any other reference to this variable will still retain the uncleared Array. – Shamasis Bhattacharya Sep 26 '12 at 12:22
show 5 more comments

If you need to keep the original array because you have other references to it that should be updated too, you can clear it without creating a new array by setting its length to zero:

A.length = 0;
share|improve this answer
4  
@Acorn Yes, it will work in all browsers. – Matthew Crumley May 10 '11 at 22:01
3  
what does ECMAScript 5 Standard says about this? – Pacerier Jun 21 '11 at 7:00
52  
@Pacerier: It still works in ES5. From section 15.4: "...whenever the length property is changed, every property whose name is an array index whose value is not smaller than the new length is automatically deleted" – Matthew Crumley Jun 21 '11 at 7:43
1  
@SteveSiebert It shouldn't in a correctly-implemented interpreter, and I don't know of any implementations that do. OF course, it's possible but it's not any more likely than other incorrect sources of leaks. – Matthew Crumley Feb 8 '12 at 20:28
1  
@LosManos Even in strict mode, length is a special property, but not read only, so it will still work. – Matthew Crumley Jan 4 at 14:18
show 10 more comments

A more cross-browser friendly and more optimal solution will be to use the splice method to empty the content of the array A as below:

A.splice(0, A.length);

share|improve this answer
12  
Why is this more cross-browser friendly? What browsers have issues with A.length? – stricjux Nov 21 '11 at 15:12
Like @alex said, splice returns a new array. Not something you would want to do in a REPL. You could always void the expression but that's not an elegant solution. – Aadit M Shah Mar 15 '12 at 14:32
This is the most correct answer since this actually "clears the array content and retains the reference to the original array object. – Shamasis Bhattacharya May 23 '12 at 8:32
2  
@jm2 what you are saying is not entirely true. It actually modifies the array in question and subsequently all references get affected. See the test on my jsFiddle: jsfiddle.net/shamasis/dG4PH – Shamasis Bhattacharya Sep 26 '12 at 12:38
2  
@alex no it does not, splice modifies the array and returns the deleted entries. Read the docs first: developer.mozilla.org/en-US/docs/JavaScript/Reference/… – David Oct 29 '12 at 16:22
show 2 more comments

You can add this to your JavaScript file to allow your arrays to be "cleared":

Array.prototype.clear = function() {
    this.splice(0, this.length);
};

list.clear();
share|improve this answer
Array.prototype.clear = function()
{
    this.length = 0;
};

and call it: array.clear();

=)

share|improve this answer

According to jsperf the fastest solution would be to pop/shift each Array Element in a for-loop.

for(var i = 0; i < A.length; i++) {
    A.pop();   //or .shift();
}

For reference: http://jsperf.com/array-destroy/11

share|improve this answer
Nice test, thanks! In IE10 (which is orders of magnitude slower in all tests), array.length = 0; is fastest. – Greg Woods Apr 11 at 12:55

Based on http://jsperf.com/array-destroy it looks like setting array.length is not the high performance solution to emptying an array, regardless of other factors.

Instead

delete array;

appears to be the more efficient solution.

share|improve this answer
This removes the datastructure as well and leaves array undefined. Using this prevents you from using array as an array after "clearing" it. – Bloodline Apr 5 at 11:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.