When using a while loop, the expression within the parentheses is evaluated each time the loop runs. Once this expression evaluates to a falsey value, the loop will terminate.
Some examples of falsey values include:
false
0
undefined
NaN
null
""
In a specific case where the value of i
is being decremented with each iteration, the loop will continue until i
reaches the value of 0
, at which point it will stop. Since it's a post-decrement operator, the comparison happens before the decrement operation. Thus, the inner loop will access values of i
ranging from someArray.length - 1
down to 0
, inclusive, which corresponds to all the indexes in that array.
Your code snippet:
var i = someArray.length;
while (i--) {
console.log(someArray[i]);
}
produces the same result as this equivalent code:
for (var i = someArray.length - 1; i >= 0; i--) {
console.log(someArray[i]);
}