admin管理员组文章数量:1328731
I've seen this in some JavaScript code but I don't get what it does.
for(;;){
//other code
}
I'm used to the for(i=0;i<someLength;i++){...}
format but I'm stuck to figure out how or what the "(;;)" syntax is for?
I've seen this in some JavaScript code but I don't get what it does.
for(;;){
//other code
}
I'm used to the for(i=0;i<someLength;i++){...}
format but I'm stuck to figure out how or what the "(;;)" syntax is for?
- duplicate? stackoverflow./q/4740979/374804 – Kris Ivanov Commented Feb 5, 2011 at 3:18
- @Iva Not an exact dup, but gets my vote since that question is actually more valuable and contains the same info. – Gordon Gustafson Commented Feb 5, 2011 at 3:28
5 Answers
Reset to default 6In JavaScript, for (;;) { ... }
just creates an infinite endless loop, which is almost exactly the same as:
while (true) {
// ...
}
or
do {
// ...
} while (true);
for (;;)
is the exact same syntax. You're allowed to leave out parts that go between the semicolons, even all of them. If you leave the condition out, it's assumed to be true
, so it results in an infinite loop. You can of course still exit with break
or return
or whatnot, hence making it not useless.
It creates a loop that runs indefinitely. It's the same syntax you're used to seeing, there's just no code between the semicolons.
The syntax for a for
loop includes an initializer, followed by a semicolon, followed by a condition for continuing the loop, followed by a semicolon, followed by code to run after each iteration of the loop.
Since there is no initializer, no condition that ever evaluates to false, and no post-loop code, the loop runs forever.
That's an infinite loop. A for
loop has 3 sections separated by ;
: the initialization section where you normally declare and define your counter's starting conditions, the middle conditional statement that evaluates to true
or false
, and the increment section.
All 3 of these sections are optional. You can include any or none. The loop continues to loop until the conditional is false
. If that condition is never met (since it was left out), it loops forever.
A place where you'll likely see it is prepended to JSON data as described by this SO post.
always true
https://developer.mozilla/en/JavaScript/Reference/Statements/for
本文标签: syntaxIn JavaScript what does for() doStack Overflow
版权声明:本文标题:syntax - In JavaScript what does for(;;){...} do? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742215512a2434494.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论