The continue statement in JavaScript

JavascriptWeb DevelopmentObject Oriented Programming

The continue statement is used for jumping over one iteration if a specific condition occurs. If a condition is met, then that iteration is skipped and continued from the next iteration.

Following is the code to implement continue statement in JavaScript −

Example

 Live Demo

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   }
   .result {
      font-size: 20px;
      font-weight: 500;
      color: blueviolet;
   }
</style>
</head>
<body>
<h1>Continue statement in JavaScript</h1>
<div class="result"></div><br />
<button class="Btn">Click Here</button>
<h3>Click on the above button to print even numbers from 1 to 30</h3>
<script>
   let resEle = document.querySelector(".result");
   let BtnEle = document.querySelector(".Btn");
   BtnEle.addEventListener("click", () => {
      for (let i = 1; i < 30; i++) {
         if (i % 2 !== 0) {
            continue;
         }
         resEle.innerHTML += i + " ";
      }
   });
</script>
</body>
</html>

Output

On clicking the ‘Click Here’ button −

raja
Published on 18-Jul-2020 11:42:51
Advertisements