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.

Answer from Gremash on Stack Overflow
🌐
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.
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!

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
Is nesting loops considered a bad practive?
Let’s take a bit of a diversion and talk about the basics of Big O notation. It’s a way of measuring how long your code will take to run. Here are a few of the key examples: O(n) - linear time - Loops are the classic example. For each extra input you have to run the loop one more time. O(n) solutions are typically pretty good, but if you have a ton of inputs they will start to slow down. Also, beware linear time solutions when you could have used something constant time (e.g. using arr.find(...) instead of obj[...]). O(logn) - log time - A binary search is the classic example. Every time you double the inputs you only add one more iteration. This is the holy grail of algorithms. Phds get paid big bucks to come up with logarithmic solutions to problems that used to be linear. O(n^2) - quadratic (or exponential) time - I usually lump quadratic (n squared) together with exponential (n to any power) because they are just different flavors of bad. A nested loop is the classic example, however, just because you have a loop in a loop does not mean you have O(n^2). Iterations have to double with each input. For example say you have an array of numbers and want to check if any is equal to any of the others. The naive O(n^2) solution would be to use a nested loop through the entire array for each element in the array. With 1000 numbers, you would need to run 1000^2 or one million iterations. At a certain point you will just crash your web browser. Okay, there is plenty more to learn about Big O and algorithms in general, but I wanted to give you a quick background so we can talk about how it impacts your problem. You have been told “nested loops are bad”, but what people really mean is “O(n^2) solutions are bad”. In your case, you have a 2d (or more) array. But if when you add a new input it's just one new element to one sub-array (as opposed to one new element to every sub-array), then what you are talking about is linear time. The nested loops are just an artifact of how you organized your data. One new input adds one more iteration, so we are talking O(n). (As others have said though, it might still be worth simplifying your data structure if it makes your code more clear and readable) More on reddit.com
🌐 r/learnjavascript
16
8
February 16, 2019
Basic JavaScript: Nesting For Loops (freeCodeCamp)

There are sub arrays in the default parameter passed to multiplyAll, it's passed multiplyAll([[1,2],[3,4],[5,6,7]]); in the code. That's an array that contains 3 sub arrays.

You need to modify the function so it does what is asked and returns the result, the exercise is setup technically that when you press the run tests buttons it will take care of sending your function proper test data (2D arrays). They come to you in the shape of the arr variable in multiplyAll. arrwill be an array with sub arrays.

Does that make sense?

More on reddit.com
🌐 r/Frontend
3
8
July 23, 2018
Nested for loops vs forEach and nested filter forEach
The difference is pretty minor, and not something you should really optimize for unless you had done some performance testing and knew that was a particular problem spot in your case. That said, with the advent of for...of in ES6, I do not use forEach much anymore. Either there is a more specific array method available (find, filter, map, etc), or a for...of loop has less boilerplate and is more clear. More on reddit.com
🌐 r/learnjavascript
20
November 22, 2020
🌐
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 - Guide to Nested Loop in JavaScript. Here we discuss an introduction to Nested Loop in JavaScript and a flowchart, syntax, and examples.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › nesting-for-loops-in-javascript
Nesting For Loops in JavaScript - GeeksforGeeks
In JavaScript, you can nest different types of loops to achieve the desired iteration. Common types include a For Loop within another For Loop, which is a standard nested loop for iterating over multi-dimensional arrays.
Published   August 5, 2025
🌐
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...
🌐
Softuni
js-book.softuni.org › chapter-06-nested-loops.html
6.1. Nested Loops · Programming Basics with JavaScript
Let's look at the example above. After initializing the first (outer) loop, its body, which contains the second (nested) loop starts executing. By itself, it prints on one row n number of stars. After the inner loop finishes executing at the first iteration of the outer one, the first loop will continue, i.e.
Find elsewhere
🌐
W3Schools
w3schools.com › js › js_loop_for.asp
JavaScript for Loop
JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Website JS Syllabus JS Study Plan JS Interview Prep JS Bootcamp JS Certificate JS Reference ... For Loops can execute a block of code a number of times.
🌐
Programiz PRO
programiz.pro › resources › js-nested-loop
Understanding Nested Loops in JavaScript
let height = 5; for (let i = height; i > 0; i--) { let row = ""; for (let j = 0; j < i; j++) { row += "* "; } console.log(row); } ... By following this step-by-step thinking process, you’ll be able to build any pattern using nested loops in JavaScript!
🌐
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
🌐
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 ...
🌐
TutorialsPoint
tutorialspoint.com › javascript › javascript_nested_loop.htm
JavaScript - Nested Loop
In the following code snippet that ... and another is an array of columns. We are using nested loop to iterate over each element of the array of rows and then iterate over each element of the array of columns....
🌐
Scaler
scaler.com › home › topics › nested loop in javascript with examples
Nested Loop in JavaScript with Examples - Scaler Topics
January 1, 2024 - You can use loop variables (e.g., i and j in the example above) and set the limits based on your requirements. JavaScript supports the concept of nested loops, where one loop is contained within another loop.
🌐
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.
🌐
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
🌐
Codedamn
codedamn.com › news › javascript
Nested for loops in JavaScript: Why?
January 4, 2023 - Hey readers, in this article we are going to discuss nested for loops in JavaScript. We will discuss How is the Nested for loop executed, Why do we use nested for loops, types of nested for loops, differences between a nested for loop and a for loop, etc.
🌐
AlgoCademy
algocademy.com › link
Nested Loops in JavaScript | AlgoCademy
In this lesson, we will explore the concept of nested loops in JavaScript. Nested loops are loops within loops, and they are particularly useful for working with multi-dimensional data structures such as arrays.
🌐
Sololearn
sololearn.com › en › Discuss › 249739 › nested-for-loops-in-js
Nested for loops in JS | Sololearn: Learn to code for FREE!
I can't figure out nested for loops. My head is spinning when I see two for loops one after the other. Can someone explain please? Am I missing a lesson or another expl
🌐
Log4JavaScript
log4javascript.org › home › js-framework › breaking down javascript nested for loops
JavaScript Nested For Loops: A Comprehensive Guide
July 4, 2023 - It has three parts: initialization, condition, and increment/decrement. Today, we delve deeper into a specialized form of the for loop – the nested for loop, which is essentially a for loop within another for loop.