3 Example(s) of JavaScript continue statement


Description :

Continue statement is used to jump over to next iteration in the loop. See the code snippet:


JavaScript continue statement Example - 1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>  
 <body>
<p id="sample"></p>

<script>
var text = "";
var i;
for (i = 0; i < 10; i++) {
    if (i === 3) { continue; }
    text +=i + "<br>";
}
document.getElementById("sample").innerHTML = text;
</script>
</body>
</html>

Output

Description :

Example of Continue statement:


JavaScript continue statement Example - 2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>  
 <body>
<p id="sample"></p>

<script>
var i = 0;
var n = 0;

while (i < 10) {
  i++;

  if (i === 2) {
    continue;
  }

  n += i;
}

document.getElementById("sample").innerHTML = n;
</script>
</body>
</html>

Output

Description :

Example of Continue statement:


JavaScript continue statement Example - 3
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>  
 <body>
 <script>
 var x = 1;
 document.write("Entering the loop<br /> ");
 while (x < 10)
 {
    x = x + 1;
               
    if (x == 5){
        continue; 
             }
        document.write( x + "<br />");
    }
    document.write("Exiting the loop!<br /> ");
         
      </script>
</body>
</html>

Output