Back to Course

## Introduction to JavaScript

0% Complete
0/0 Steps
1. JavaScript: Basics
9Topics
|
10 Quizzes
2. JavaScript: Variables
9Topics
|
10 Quizzes
3. JavaScript: Conditional Statements
10Topics
|
11 Quizzes
4. JavaScript: Functions
10Topics
|
11 Quizzes
5. JavaScript: Scope
6Topics
|
7 Quizzes
6. JavaScript: Arrays
11Topics
|
12 Quizzes
7. JavaScript: Loops
8Topics
|
9 Quizzes
8. JavaScript: High Order Functions
3Topics
|
4 Quizzes

#### Participants 24

In Progress
Lesson 7, Topic 5
In Progress

# JavaScript: Nested Loops

##### Yasin Cakal 20 Nov 2021
Lesson Progress
0% Complete

A nested loop is defined as a loop that runs inside another loop. A nested `for` loop can be used to compare the elements of two arrays. The inner `for` loop will run completely after each round of the outer `for` loop.

Consider the following example of a nested for loop:

``````const arrayA = [11, 19, 21, 33];
const arrayB = [15, 21, 29,44,45];
for (let i = 0; i < arrayA.length; i++) {
for (let j = 0; j < arrayB.length; j++) {
if (arrayA[i] === arrayB[j]) {
console.log('Both loops have the number: ' + arrayB[j])
}
}
};

//OUTPUT: Both arrays have the number: 21``````

Consider what’s going on in the nested loop in our example. The inner loop will run in its entirety for each element in the outer loop array, `arrayA`, comparing the current element in the outer array, `arrayA[i]`, to each element in the inner array, `arrayB[j]`. It prints a string to the console when it finds a match.

Quizzes