let stars = ' '; If you initialize the string with a space in it, with the plan to concatenate stars, then the result will be * ** *** and so on, instead of * ** *** which, while only a small difference, is technically wrong. for (let col = 0 ; col < row.length; col++){ A couple things he… Answer from colinthornton on forum.freecodecamp.org
🌐
Softuni
js-book.softuni.org › chapter-06-nested-loops.html
6.1. Nested Loops · Programming Basics with JavaScript
This is repeated until the variable of the outer loop meets the condition to end the loop. Here is an example that illustrates nested loops.
🌐
Softuni
js-book.softuni.org › chapter-06-nested-loops-exam-problems.html
6.2. Nested Loops – Exam Problems · Programming Basics with JavaScript
The lower part of the figure, in which the width of the sign decreases, can be done by creating a loop, that iterates n number of times. The structure of a row should have a beginning . + \\, middle part _ and an end // + .. The number of the dots in the first loop iteration has to be 0 and ...
Discussions

Nested For Loops
Hey all. First time poster on FCC. I tried algorithm scripting with high difficulties like others. So I wanted to take a step back and look at nested for loops to increase my understanding. I’m attempted to printout a so… More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
July 22, 2020
Javascript "Nested Loops" - exercise 6
Hey all, first of all thanks for looking at my post, hopefully you can answer my question. Exercise Link: https://www.codecademy.com/courses/introduction-to-javascript/lessons/loops/exercises/for-loops-iii Please see m… More on discuss.codecademy.com
🌐 discuss.codecademy.com
0
0
March 18, 2020
Understanding nested for loops in javascript - Stack Overflow
I'm learning JavaScript at the moment on freecodecamp and they have an example for nested for loops in one of their excercises: More on stackoverflow.com
🌐 stackoverflow.com
Understanding Nested For Loops in JavaScript Arrays - LambdaTest Community
I’m currently learning JavaScript on FreeCodeCamp, and I encountered an example involving nested for loops in JavaScript in one of their exercises: var arr = [[1, 2], [3, 4], [5, 6]]; for (var i = 0; i More on community.lambdatest.com
🌐 community.lambdatest.com
0
October 24, 2024
🌐
EDUCBA
educba.com › home › software development › software development tutorials › javascript tutorial › nested loop in javascript
Nested Loop in JavaScript | Guide to Nested Loop - Flowchart & Examples
June 28, 2023 - If it is true, then the inner loop is executed again, and these steps will be repeated until the condition in the outer loop is true. JavaScript supports the nested loop feature, where a loop is present inside another loop.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-conditional-statements-and-loops-exercises.php
JavaScript conditional statements and loops - Exercises, Practice, Solution - w3resource
July 10, 2025 - Write a JavaScript program that iterates integers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for multiples of five print "Buzz". For numbers multiples of both three and five print "FizzBuzz". ... "Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
🌐
Exercism
exercism.org › tracks › javascript › concepts › for-loops
For Loops in JavaScript on Exercism
For loops can be nested, for example to iterate over nested (multi-dimensional) arrays.
Find elsewhere
🌐
AlgoCademy
algocademy.com › link
Nested Loops in JavaScript | AlgoCademy
Learn "Nested Loops in JavaScript" with our free interactive tutorial. Master this essential concept with step-by-step examples and practice exercises.
🌐
Basescripts
basescripts.com › mastering-javascript-nested-loops-coding-exercises-and-quiz-questions-test-your-knowledge
Mastering JavaScript Nested Loops Coding Exercises and Quiz questions Test your Knowledge – Coding Help Tips Resources Tutorials
December 5, 2023 - Understand the structure of nested loops. Utilize them for various tasks: printing patterns, handling arrays, and more. Be mindful of performance considerations. ... Nested loops are loops within loops, allowing for more complex iterations through ...
🌐
Khan Academy
khanacademy.org › computing › computer-programming › programming › looping › pt › nested-for-loops
Nested For Loops | Looping | Intro to JS
JavaScript is disabled in your browser · Please enable JavaScript to proceed · A required part of this site couldn’t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
🌐
Scientech Easy
scientecheasy.com › home › blog › nested for loops in javascript
Nested For Loops in JavaScript - Scientech Easy
August 14, 2025 - Learn nested for loops in JavaScript, basic syntax, example programs based star, numbers, and alphabet design patters using nested for loops
🌐
Codecademy Forums
discuss.codecademy.com › get help › javascript
Javascript "Nested Loops" - exercise 6 - JavaScript - Codecademy Forums
March 18, 2020 - Hey all, first of all thanks for looking at my post, hopefully you can answer my question. Exercise Link: https://www.codecademy.com/courses/introduction-to-javascript/lessons/loops/exercises/for-loops-iii Please see my code below. I can can log the resulant array to the console, yet I get a ‘Did you write a nested for loop?’ error. let bobsFollowers = ['Ash','Barry','Calum','Denny']; let tinasFollowers = ['Ash','Denny','Rob']; let mutualFollowers = []; for (i = 0 ; i
🌐
Teaching-materials
teaching-materials.org › javascript › exercises › forloops
JavaScript 1: for loops
Write a for loop that will iterate from 0 to 10. For each iteration of the for loop, it will multiply the number by 9 and log the result (e.g. "2 * 9 = 18"). Bonus: Use a nested for loop to show the tables for every multiplier from 1 to 10 (100 results total).
Top answer
1 of 7
21
var arr = [[1,2], [3,4], [5,6]];

This is an array of arrays. It is a little bit easier to read like this:

var arr = [
            [1,2],
            [3,4],
            [5,6]
          ];

That makes it a little bit easier to see that you have an array of 3 arrays. The outer 'for' will loop through each of 1st level arrays. So the very first outer for loop when i=0 you are going to grab the first inner array [1,2]:

for (var i=0; i < arr.length; i++) {
    //First time through i=0 so arr[i]=[1,2];
}

In the inner loop you are going to loop through each of the 3 inner arrays one at a time.

for (var j=0; j < arr[i].length; j++) {
    //Handle inner array.
}

This argument grabs the length of the inner array:

arr[i].length

So on your first time through the outer loop i=0 and arr[i] is going to equal [1,2] because you are grabbing the 0th element. Remember, arrays elements are always counted starting at 0, not 1.

Finally you are printing out the results with:

console.log(arr[i][j]);

The first time through you can break it down a little. i=0 and j=0. arr[0][0] which translates as grab the first element from the outer array and then the first element from the first inner array. In this case it is '1':

[
    [1,2], <-- 0
    [3,4], <-- 1
    [5,6]  <-- 2
];

The code will loop through the first first set [1,2], then the second [3,4], and so on.

2 of 7
9

The double for loop you have above works like so:

 var arr = [[1,2], [3,4], [5,6]];

 for (var i=0; i < arr.length; i++) {
  // i = 0, then we loop below:
  for (var j=0; j < arr[i].length; j++) {
    //here we loop through the array which is in the main array
    //in the first case, i = 0, j = 1, then we loop again, i = 0, j = 1
    console.log(arr[i][j]);
    //after we finish the stuff in the 'j' loop we go back to the 'i' loop 
    //and here i = 1, then we go down again, i, remains at 1, and j = 0, then j = 1
    //....rinse and repeat, 
  }
}

In plain english:

We grab the first element in the main array, which is an array itself, we loop through that, and log at each index, this is terminated by our length condition in the second loop. We then move to to the next index of the main array, which is an array itself.... and so on, until we reach the end of the main array

To access and index in the main array, we need to use array[i] - that index holds an array - so to go INTO that array, we need to use array[i][j]

Hope that makes sense!

🌐
Coding Rooms
codingrooms.com › blog › nested-loops-javascript
Nested Loops JavaScript
October 6, 2020 - The inner loop prints the individual digits while the outer loop determines how many times this task is repeated. Try changing counter < 4 to counter < 1. The numbers 1 through 3 should only be printed once if this change is made.
🌐
freeCodeCamp
freecodecamp.org › news › nesting-for-loops-in-javascript
Nesting For Loops in JavaScript
June 2, 2020 - If you're having trouble understanding freeCodeCamp's Nesting For Loops challenge, don't worry. We got your back. In this problem you have to complete the multiplyAll() function, and takes a multi-dimensional array as an argument. Remember that a mul...
🌐
LinkedIn
linkedin.com › pulse › mastering-javascript-nested-loops-coding-exercises-quiz-svekis--g3ngc
Mastering JavaScript Nested Loops Coding Exercises and Quiz questions Test your Knowledge
December 5, 2023 - Get PDF guide with Exercises and Quiz questions https://basescripts.com/mastering-javascript-nested-loops-coding-exercises-and-quiz-questions-test-your-knowledge Nested Loops in JavaScript Nested loops are loops within loops, allowing for more complex iterations through data structures like arrays o
🌐
LambdaTest Community
community.lambdatest.com › general discussions
Understanding Nested For Loops in JavaScript Arrays - LambdaTest Community
October 24, 2024 - I’m currently learning JavaScript on FreeCodeCamp, and I encountered an example involving nested for loops in JavaScript in one of their exercises: var arr = [[1, 2], [3, 4], [5, 6]]; for (var i = 0; i < arr.length; i++…
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › nesting-for-loops-in-javascript
Nesting For Loops in JavaScript - GeeksforGeeks
Exercise · Last Updated : 5 Aug, ... or performing complex tasks. It involves placing one loop inside another, where the outer loop executes for each iteration of the inner loop....
Published   August 5, 2025
🌐
W3Schools
w3schools.com › jsref › jsref_for.asp
JavaScript for Statement
A nested loop (a loop inside a loop): let text = ""; for (let = 0; i < 3; i++) { text += i + "<br>"; for (let j = 10; j < 15; j++) { text += j + "<br>"; } } Try it Yourself » · for is an ECMAScript1 (JavaScript 1997) feature.