最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Omitting Condition in For Loop - Stack Overflow

programmeradmin0浏览0评论

Can someone please help me understand why the following code works:

var someNumbers = [1,2,3,4,5];
var length = someNumbers.length;

for(var i=length; i--;) {
  console.log(i);
}

How does this for loop know to terminate once i equates to 0? What about negative numbers? Wouldn't this cause an infinite loop?

Can someone please help me understand why the following code works:

var someNumbers = [1,2,3,4,5];
var length = someNumbers.length;

for(var i=length; i--;) {
  console.log(i);
}

How does this for loop know to terminate once i equates to 0? What about negative numbers? Wouldn't this cause an infinite loop?

Share Improve this question asked Oct 10, 2014 at 13:27 markmillermarkmiller 1601 gold badge1 silver badge10 bronze badges 1
  • Relevant: stackoverflow./q/5289472/1317805. – James Donnelly Commented Oct 10, 2014 at 13:30
Add a ment  | 

3 Answers 3

Reset to default 7

In Javascript, anything can be a condition! In this case, it's i--. Once i-- returns 0 the loop will stop because 0 is falsy.

The part that's missing is the third expression (the "final expression", see MDN page on the for loop) where you usually find the increment/decrement operation, but both are bined in the "condition" expression in this case.

This is how for loops work. MDN covers for loops in a lot of detail, but to paraphrase:

Syntax

for ([initialization]; [condition]; [final-expression]) statement

condition

An expression to be evaluated before each loop iteration. If this expression evaluates to true, statement is executed. This conditional test is optional. If omitted, the condition always evaluates to true. If the expression evaluates to false, execution skips to the first expression following the for construct.

0 in JavaScript is falsy - this means that it evaluates to false when evaluated as a Boolean expression.

What your for loop essentially does is assign the value stored in your length variable to your i variable, then subtracts 1 from it every time the loop iterates. Eventually, your i variable will be equal to 0. Here it will stop looping and move on to the next statement after the for loop.

This would create an infinite loop if your length variable was negative, but that shouldn't happen with the code you've provided.

The decrement method mentioned is for

 for(var i=length; i--;) {
    console.log(i);
     }

Is there a performance difference between for (var i=length; i--;) &

 for (var i=length; i>0; i--)
    {
    console.log(i);
    }

Those are not equivalent. The first loop goes through length-1 to 0 inclusively while the second one goes length to 1. The first loop is easy to be turn in infinite loop. If the value of the length is less than 0 the loop is infinite. It is definitely better to use the second form. Even it is longer it is safer and more readable version.

发布评论

评论列表(0)

  1. 暂无评论