![]() Then the JavaScript label can be used by writing break or continue followed by the label_name. To use JavaScript labels, you must first define a label, which is nothing but a name followed by a colon( :) and then your code. Where the break and continue statements can only be used to break or skip iteration of a code block in a loop or switch case, using a label with break and continue statement lets you control the execution of any code block. JavaScript Labelsīy using the JavaScript labels you can control the flow of your code's execution more precisely. ![]() Hence you can see that the document.write code is executed for only those iterations where the value of the count variable was not divisible by 2, which means for odd values only. In the code example above, whenever the value of the variable count is divisible by 2, we do nothing and call the continue statement to directly jump to the next iteration of the loop. This is used in case, for some specific condition in the loop execution you want to perform no action and directly jump to next iteration then the continue statement can be used. The continue statement can be used to skip the current loop iteration and jump to the next iteration if the loop condition is satisfied. Try removing the break statement and you will see the loop will get executed 10 times. In the above example, when the value of the variable count becomes divisible by 5, we break out of the loop, and hence the loop execution stops. Let us recall the break statement once again, the break statement lets us break out of the loop execution or any switch statement and move on to execute the code after the end of the loop or switch. You may recall that the break statement was introduced in switch case, this statement can also be used inside other loops like for loop and while/do-while loops. Illegal break statement Exception in JavaScript JavaScript break Statement
0 Comments
Leave a Reply. |