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

for instance if

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

how can I empty that.

share|improve this question
21  
Please accept a different answer, the accepted answer is just wrong – Juan Mendes Nov 5 '12 at 20:44
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

5 Answers

up vote 85 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
277  
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
11  
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
9  
34 upvotes and 17 downvotes. Doesn't that deserve a special badge? :-) – Philippe Leybaert Jan 17 '12 at 15:50
2  
arr = new Array() is exactly the same as arr = [] – Philippe Leybaert May 16 '12 at 12:32
11  
-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
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
Should this work with all browsers? – Acorn May 10 '11 at 21:22
3  
@Acorn Yes, it will work in all browsers. – Matthew Crumley May 10 '11 at 22:01
2  
what does ECMAScript 5 Standard says about this? – Pacerier Jun 21 '11 at 7:00
41  
@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
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
10  
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

Your Answer

 
or
required, but never shown
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.