62

I'm trying to empty an array containing my drawn coordinates when a button "clear" is pressed.

When I call drawnDivs.clear(), I get an error that it is not a function. drawnDivs is certainly an array, and I have Firebug console.logs printing things out. It's hosted here.

4
  • 1
    Out of interest, where did you get the idea to use clear()? It doesn't appear to be a part of Javascript. Commented Oct 26, 2010 at 3:55
  • Ah looks like visual basic :) Commented Oct 26, 2010 at 3:58
  • a google search got me here: roseindia.net/java/javascript-array/… Commented Oct 26, 2010 at 4:20
  • readers might be interested in Array.prototype.fill eg. myArray.fill(null) Commented Jul 31, 2024 at 9:52

5 Answers 5

81

Nope, it's not. But drawnDivs.length = 0 should work.

Sign up to request clarification or add additional context in comments.

5 Comments

This will work if it's a true array, and not just an array-like object.
@thomasrutter. True but it will preserve references and any other addition properties and functions defined on the array.
yes, this is still the best solution overall. And to be honest, it would still work on array-like objects, it just wouldn't free any of the existing members from memory right away. So only a minor point really.
@thomasrutter -an "arraylike" object, is not an Array, to begin with - so...(?!) .
Objects that can be used as if they are arrays but which aren't arrays are fairly common in JavaScript. If you want your array handing code to work with "real" arrays as well as objects which can act as arrays, then you have to make sure it can cope with the latter.
9

drawnDivs = [];

1 Comment

Be careful, since this will not modify other references to the object.
4

It was answered in Stack Overflow question How do I empty an array in JavaScript?.

Two examples from the answer:

var A = ['some', 'values', 'here'];

//Method 1

//(This was my original answer to the question)

A = [];




// Method 2 (as suggested by Matthew Crumley)

A.length = 0

And here is a nice write up on these two methods by Dr. Axel Rauschmayer.

1 Comment

The chosen answer in the referred link is not the right solution as it does not clear the variables that refer to the array being cleared. The correct answer in that thread is stackoverflow.com/a/8134354/206687
2

An optimized way to do it is:

while (arr.pop()) {}

See http://jsperf.com/kbk-clear-array/2.

2 Comments

This fails if the array has empty slots. This should do it: while (arr.length > 0) { arr.pop(); }
No, this should be: ` while(arr.length){ arr.pop(); }`
0

You could alternately use the Prototype library and then, use Prototype's clear() method.

2 Comments

Could be an alternative way, but i think there are a lot of easier ways without the use of libraries to clear an array.
Yea -- that would work, but it is using a sledgehammer to kill a fly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.