admin管理员组文章数量:1221766
Is it possible to leave out the variable assignment from a for loop and do something like this…?
otherVar = 3;
for ( otherVar > 0; otherVar-- )
{
stuff
}
Is it possible to leave out the variable assignment from a for loop and do something like this…?
otherVar = 3;
for ( otherVar > 0; otherVar-- )
{
stuff
}
Share
Improve this question
asked May 23, 2012 at 3:23
EssentialEssential
4217 silver badges14 bronze badges
1
- 1 You can also declare a bunch of vars right in the loop: for(var someVar=0, otherVar=3, yetAnother='bob';yetAnother!==false;someVar++) – Erik Reppen Commented May 23, 2012 at 3:42
3 Answers
Reset to default 16Yes, but you need to put in the semi-colon:
var otherVar = 3;
for ( ; otherVar > 0; otherVar-- ) {
doStuff();
}
Usually While is more popular for this situation (better readability)..
otherVar = 3;
while ( otherVar > 0)
{
stuff
otherVar--;
}
You can count down from any arbitrary number:
var counter = 3;
while ( counter-- ) {
console.log( counter );
}
Which outputs: 2, 1, 0
本文标签: javascriptuse existing variable in for loopStack Overflow
版权声明:本文标题:javascript - use existing variable in for loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739260198a2155329.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论