- break: it will break the on going loop and exit.
- continue: once encounter continue, it will skip the remaining code block and continue running loop.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <p id="div1"></p> <script type="text/javascript"> const fruits = ["apple", "banana", "orange", "guava"]; let text = ""; for (let i in fruits) { if (fruits[i] == "apple") { continue; } if (fruits[i] == "orange") { break; } text += fruits[i] + "<br>"; } document.getElementById("div1").innerHTML = text; </script> </body> </html> |
output: banana
in above code , continue during apple will get skipped and loop will break once encounter orange.