implement a three nested loop in Javascript
Code snippet on how to implement a three nested loop in Javascript
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
for (let k = 0; k < 3; k++) {
console.log(i, j, k);
}
}
}
This code is a three nested loop in Javascript. The outermost loop is represented by for (let i = 0; i < 3; i++)
, meaning it will loop from 0 to 2 with the increase of one on each loop. Inside this loop, we have the second loop for (let j = 0; j < 3; j++)
which will run from 0 to 2.
The innermost loop is for (let k = 0; k < 3; k++)
, which will also loop from 0 to 2 in the same manner as the previous two loops. The console.log(i, j, k)
statement will allow us to see the output of the combination of i
, j
and k
on each loop. In other words, the three nested loop will run 3 x 3 x 3 = 27 times in total and each time it will log to the console the combination of i
, j
, and k
.