JavaScript Empty an Array
My favorite answer is:
while (A.length > 0) {
A.pop();
}
I strongly suggest to read the discussions of the Stack Overflow question [2]. The reason why I like the answer above is that long time ago, I had to remove all child nodes of a DOM element [3], the bug-free way to do this is:
while (elm.hasChildNodes()) {
elm.removeChild(elm.lastChild);
}
Compare the idea of remove all child nodes and empty an array in above code, it is very clearly that the idea is the same. This is an interesting finding so I make it a post.
References:
[1] |
[2] | How do I empty an array in JavaScript? - Stack Overflow |
[3] | JavaScript Remove All Children of a DOM Element |