Maybe this is a dumb question but is there a way to return to the top of a loop?
Example:
for(var i = 0; i < 5; i++) {
if(i == 3)
return;
alert(i);
}
What you’d expect in other languages is that the alert would trigger 4 times like so:
alert(0);
alert(1);
alert(2);
alert(4);
but instead, the function is exited immediately when i is 3. What gives?
Use
continueinstead ofreturn.Example: http://jsfiddle.net/TYLgJ/
If you wanted to completely halt the loop at that point, you would use
breakinstead ofreturn. Thereturnstatement is used to return a value from a function that is being executed.EDIT: Documentation links provided by @epascarello the comments below.
continue: https://developer.mozilla.org/en/JavaScript/Reference/Statements/continuereturn: https://developer.mozilla.org/en/JavaScript/Reference/Statements/return