I was wondering if there was anything special about javaScript's pop() function that I should know about.
My intention was to write a program that erases all of the values in an array, without actually deleting the array. My first attempt at that was this, which didn't exactly work out:
var index = [1,2,3,4,5,6,7,8,9,10];
for(var i=0;i<index.length;i++) { index.pop(); console.log(index+';'); }
// outputs this:
[1,2,3,4,5,6,7,8,9];
[1,2,3,4,5,6,7,8];
[1,2,3,4,5,6,7];
[1,2,3,4,5,6];
[1,2,3,4,5];
And then it simply stops there. Huh? Upon checking I found that the array really was [1,2,3,4,5], and not some console.log display error. Which didn't make sense.
On a hunch, I decided to decrement the for loop instead, and use it like this:
var index = [1,2,3,4,5,6,7,8,9,10];
for(var i=index.length; i--; i>0) { index.pop(); console.log(index+';'); }
// and for some reason, this works:
[1,2,3,4,5,6,7,8,9];
[1,2,3,4,5,6,7,8];
[1,2,3,4,5,6,7];
[1,2,3,4,5,6];
[1,2,3,4,5];
[1,2,3,4];
[1,2,3];
[1,2];
[1];
[];
So, why does the second example work and not the first? Seems pretty strange to me.
EDIT: I didn't know the for loop looked for index.length every time, not just the first time it was called.
i<index.length- you understandiis being incremented on every iteration (because ofi++), andindex.lengthis technically decrementing on every iteration (because ofindex.pop())....right? That means at some point sooner than you expected,i<index.lengthwill no longer be true and the loop will exit. If you want to make sure you loop over the array the amount you're expecting, you need to storeindex.lengthonce at the beginning. And by the way, this isn't something special with JavaScript. I'm pretty sure this would've happened in any languageindex.lengthchanges every iteration. Though, if you want to delete all elements of an array but still want the variable to be an array, why not just reassign the variable to an empty arrayindex = [];?for(var i=index.length; i--; i>0)is unnecessary. The second expression is always the condition. The third expression is always executed at the end of an iteration and typically advances the loop variable. You are only making a comparison which doesn't do anything, so it's equivalent tofor(var i=index.length; i--; )whilefor this may be simplerwhile (array.length) { array.pop(); }and of course there isarray.length = 0. Just some additional thoughts. :)