Loop in JavaScript

Loop in JavaScript is used to execute some block of code, multiple times based on the condition/requirement. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>
   
   <script>
      let x = 2;
      for(let i=1; i<=10; i++)
         console.log(x*i);
   </script>
   
</body>
</html>

The output produced by above JavaScript example is shown in the snapshot given below:

javascript loop example

In above example, the following statement:

console.log(x*i);

will be executed for 10 times with value of i from 1 to 10. The detailed description, about how a for loop executes, is given in its separate tutorial.

List of All Types of Loop in JavaScript

  1. for Loop
  2. while Loop
  3. do...while Loop

JavaScript Online Test


« Previous Tutorial Next Tutorial »