Is JavaScript's array.clear() not a function?

JavascriptArrays

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

drawnDivs = [];

Solution 3 - Javascript

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.

Solution 4 - Javascript

An optimized way to do it is:

while (arr.pop()) {}

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

Solution 5 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNona UrbizView Question on Stackoverflow
Solution 1 - JavascriptjordanbtuckerView Answer on Stackoverflow
Solution 2 - JavascriptMike RuhlinView Answer on Stackoverflow
Solution 3 - JavascriptsubhazeView Answer on Stackoverflow
Solution 4 - Javascriptuser3271659View Answer on Stackoverflow
Solution 5 - JavascriptDebosmit RayView Answer on Stackoverflow