Nested For Loop in JavaScript


In JavaScript, a nested "for" loop is a loop inside another loop. It allows you to create more complex looping structures to handle multidimensional data or perform repetitive tasks involving multiple sets of data.

Syntax:
       for (let i = 0; i < outerLength; i++)
       {
              // Code to be executed in the outer loop
              for (let j = 0; j < innerLength; j++)
              {
                     // Code to be executed in the inner loop
              }
       }


Source Code

HTML Code
<!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>Tutor Joes</title>
</head>
<body>
 
	<script src="js/script.js"></script>
</body>
</html>
To download raw file Click Here

script.js
let nums=[];
for(let i=0;i<3;i++) //i=0 0<3 1<3
{
  nums.push([]); //nums[0] nums[1]
  for(let j=0;j<3;j++)
  {
    nums[i].push(j);//num[1]={0,1,2}
  }
}

console.log(nums);
console.table(nums);
To download raw file Click Here

List of Programs


JS Practical Questions & Answers


JS Practical Project