YouTip LogoYouTip

Js Break

## JavaScript break and continue Statements\\ \\ JavaScript in the `break` and `continue` StatementsUsed to control the execution flow of loops.\\ \\ ### break Statements\\ \\ `break` StatementsUsed to exit the entire loop (for, while, do...while)。\\ \\ #### Example\\ \\ ```javascript\\ for (i = 0; i < 10; i++) {\\ if (i === 3) {\\ break;\\ }\\ console.log(i);\\ }\\ ```\\ \\ The output result is:\\ \\ ```\\ 0\\ 1\\ 2\\ ```\\ \\ ### continue Statements\\ \\ `continue` StatementsUsed to skip the current iteration in the loop and continue to the next iteration.\\ \\ #### Example\\ \\ ```javascript\\ for (i = 0; i < 10; i++) {\\ if (i === 3) {\\ continue;\\ }\\ console.log(i);\\ }\\ ```\\ \\ The output result is:\\ \\ ```\\ 0\\ 1\\ 2\\ 4\\ 5\\ 6\\ 7\\ 8\\ 9\\ ```\\ \\ ### break and continue in nested loops\\ \\ In nested loops,`break` and `continue` Only affects the layer of loop they are in.\\ \\ #### Example\\ \\ ```javascript\\ for (i = 0; i < 3; i++) {\\ for (j = 0; j < 3; j++) {\\ if (j === 1) {\\ continue;\\ }\\ console.log("i=" + i + ", j=" + j);\\ }\\ }\\ ```\\ \\ The output result is:\\ \\ ```\\ i=0, j=0\\ i=0, j=2\\ i=1, j=0\\ i=1, j=2\\ i=2, j=0\\ i=2, j=2\\ ```\\ \\ ```javascript\\ for (i = 0; i < 3; i++) {\\ for (j = 0; j < 3; j++) {\\ if (j === 1) {\\ break;\\ }\\ console.log("i=" + i + ", j=" + j);\\ }\\ }\\ ```\\ \\ The output result is:\\ \\ ```\\ i=0, j=0\\ i=1, j=0\\ i=2, j=0\\ ```\\ \\ ### Use labels to control the loop\\ \\ You can use labels to control the execution of the loop, which is especially useful in nested loops.\\ \\ #### Example\\ \\ ```javascript\\ myLoop: for (i = 0; i < 3; i++) {\\ for (j = 0; j < 3; j++) {\\ if (j === 1) {\\ continue myLoop;\\ }\\ console.log("i=" + i + ", j=" + j);\\ }\\ }\\ ```\\ \\ The output result is:\\ \\ ```\\ i=0, j=0\\ i=1, j=0\\ i=2, j=0\\ ```\\ \\ ### Summary\\ \\ - `break` Used to completely exit the loop.\\ - `continue` Used to skip the current iteration and continue to the next iteration.\\ - In nested loops,`break` and `continue` Only affects the innermost loop.\\ - You can use labels to control more complex loop behaviors.
← Js ErrorsJs Loop While β†’