Pitch your project

Random text


By admin Category Uncategorized, Posted February 12th, 2016
When and how to use (nested) for-loops

For loops are frequently used in multiple programming languages. A simple for loop allows us to repeat the same code multiple times. Rather than writing your code over and over again, your for loop does that for you and, moreover, is less prone to make mistakes. For example, it can do simple calculations in JavaScript:

var tableOfNine=[];

for (var i = 1; i < 11; i++){


Using a single for loop

A practical way of using for loops is when you have to loop trough an array find information. In the following array, called var persons, I’ve added four people, or objects, and have given them two properties: a name and a social security number (ssn):

var persons = [
{name: "Adam", ssn: 1},
{name: "Eve", ssn: 2},
{name: "Cain", ssn: 3},
{name: "Abel", ssn: 4}

Now, let’s say we are interested in the properties of only the first two objects (or persons) in our array. We can use a simple for loop:

for (var i = 0; i < 2; i++){

If we check the console, we can see that we got the properties of the first two objects in our array, with each a name and a ssn.

What if we look through out data and see we have another array called familyNames:

var familyNames = [
{lastName: "Johnson", ssn: 4},
{lastName: "Smith", ssn: 2},
{lastName: "McDonald", ssn: 1},
{lastName: "de Boer", ssn: 3}

Using a second for loop

We can see that this array only contains a person’s family name while the first array only contains first names. Luckily, since social security numbers are unique for each person, we can use two for loops to match the first name and last name of a person:

for (var i = 0; i < persons.length; i++) {

    for (var j = 0; j < familyNames.length; j++) {

        if (persons[i].ssn === familyNames[j].ssn) {

            console.log (persons[i].firstName + " " + familyNames[j].lastName);

In the code above we used a first for loop to loop through the objects in the array persons using the variable i. Inside the for loop we have nested a second for loop that loops trough the objects in the array familyNames using variable j. This is also called a nested for loop. Then, we use an if statement to compare the ssn values. If the ssn in both objects is equal, we know that the first name from the array persons and the last name from the array familyNames. Finally we console log the first and last name together to get the full name of each person.

In conclusion

The basic idea behind a for loop is repeating the same piece of code while only changing one parameter over an interval of values. When you want to change more parameters, you can use nested for loops, which basically are loops within loops. In our example we used to for loops to search trough two arrays and combined a persons first and last name using their social security number. Of course you can use for loops to do much more complex code, but the basic idea remains the same.

Posted by Danny Cornelisse

Leave a Reply

Your email address will not be published. Required fields are marked *