Jump statements in JavaScript include: 1. break statement, syntax "break;"; 2. continue statement, which can only be used in the loop body of a loop statement, syntax "continue;"; 3. return statement , syntax "return [expression]".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Jump statements can jump out of the branch, loop, or statement returned from a function call.
JavaScript’s jump statements include three types: break statement, continue statement, and return statement.
break statement is used to exit a loop or switch statement. Its syntax format is as follows.
break;
[Example 1] In the following example, set the loop expression of the while statement to always be true (while can convert the value 1 to true). Then set an if statement in the while loop structure to determine when the variable i is greater than 50, then jump out of the while loop body.
var i=0; while(1){ if(i>50)break; i++; document.write(i); }
[Example 2] Jump statements can also be used in conjunction with markers to jump to a specified line instead of just jumping out of the loop body. In the nested for loop body below, define a mark The usage of the
continue statement is similar to the break statement. The only difference is that the continue statement does not exit the loop, but starts it. A new iteration (i.e. re-executing the loop statement). Regardless of whether it is marked or unmarked, the continue statement can only be used within the loop body of a loop statement.
return statement is used to specify the return value of a function. It can only be used in functions or closures. The statement form is as follows:
x:for (a=1;a<10;a++) { //添加标签 document.write("<br />"+a+"<br />"); for(var b=1;b<10;b++){ if(a>5) break x; //如果a大于5,则跳出标签 document.write(b); } }
return statement is executed, the expression expression is first calculated, then the value of the expression is returned, and the control logic is returned from the function body.
[Recommended learning:javascript advanced tutorial]
The above is the detailed content of What are the jump statements in javascript. For more information, please follow other related articles on the PHP Chinese website!