The nested for loop in JavaScript

A simple for loop executes a specified number of times depending on the initialization value and the terminating condition. A nested for loop on the other hand, resides one or more for loop inside an outer for loop.

In a nested loop the statement inside the for loop body is again a for loop. This causes The inside for loop to execute all the way through , for each iteration of the outer for loop.

for(let i = 0 ; i < limit; i++)
{
   for(let j = 0 ; j < limit; j++)
   {
      // statement
   }
   // statement for outer loop
}

The inside loop in this example runs limit number of times for every iteration of the outer loop. So, in total, the loop runs limit x limit number of times.

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript</title>
  <script>
    for(var a = 1; a <= 5; a++){
      for(var b = 1; b <= a ; b++){
        document.write(a +  " ");
      }
      document.write("<br>");
    }
  </script>
</head>
<body>
</body>
</html>