Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

This question already has an answer here:

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.

share|improve this question

marked as duplicate by nbrooks, Sirko javascript Aug 3 '14 at 7:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
Out of interest, where did you get the idea to use clear()? It doesn't appear to be a part of Javascript. – thomasrutter Oct 26 '10 at 3:55
    
Ah looks like visual basic :) – thomasrutter Oct 26 '10 at 3:58
    
a google search got me here: roseindia.net/java/javascript-array/… – Nona Urbiz Oct 26 '10 at 4:20
up vote 60 down vote accepted

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

share|improve this answer
1  
This will work if it's a true array, and not just an array-like object. – thomasrutter Oct 26 '10 at 3:54
1  
@thomasrutter. True but it will preserve references and any other addition properties and functions defined on the array. – jordanbtucker Oct 26 '10 at 4:13
    
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 Oct 26 '10 at 12:43

drawnDivs = [];

share|improve this answer
22  
Be careful, since this will not modify other references to the object. – Josh Lee Oct 26 '10 at 3:59

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.

share|improve this answer
    
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 – Shamasis Bhattacharya Sep 26 '12 at 13:04
    
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Claies May 4 '14 at 1:32
    
@AndrewCounts done. Thanks for the nudge. – subhaze May 5 '14 at 20:13

An optimized way to do it is:

while (arr.pop()) {}

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

share|improve this answer
1  
This fails if the array has empty slots. This should do it: while (arr.length > 0) { arr.pop(); } – Geoff May 21 '15 at 13:55
    
No, this should be: ` while(arr.length){ arr.pop(); }` – Jorge Riv Dec 10 '15 at 15:47

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

share|improve this answer
1  
Could be an alternative way, but i think there are a lot of easier ways without the use of libraries to clear an array. – Ruben-J Sep 23 '12 at 19:41
1  
Yea -- that would work, but it is using a sledgehammer to kill a fly. – Jeremy J Starcher Sep 23 '12 at 20:16

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