erase: function(item){
for (var i = this.length; i--;) {
if (this[i] === item) this.splice(i, 1);
}
return this;
}
How would this (var i = this.length; i--;) syntactic construct work?
Should there be three sections? What is it in this case?
from here.
It initializes
itothis.length, and uses the condition check to also decrement it. When it gets to zero, which is a falsy value, the loop stops.But note that because you decrement in the check, the loop starts at
this.length - 1and ends at0. So it’s equivalent to:But please refrain from using this kind of syntax, as its behavior is confusing – as you might have noticed.