For loops in Javascript

Learning to code can be compared to learning to cook your favorite dishes. Just like how you master the art of making a perfect biryani by repeatedly practicing the recipe, coding requires you to grasp the basics and then refine your skills over time. Today, we’re going to dive deep into one of the most fundamental concepts in JavaScript: the for
loop. By the end of this article, you’ll have a solid understanding of how to use for
loops effectively, and you’ll be able to relate them to real-life scenarios.
Nothing works perfectly the first time, even fried rice is usually "burnt rice" when you make it the first time, so if you find it hard to understand, go into a loop of reading these series of articles on loops again and again and ensure you understand it properly, this is not only a great concept to learn, it is indeed a fundamental programming structure, it will be your daily bread and butter and not to mention its significance in learning Data structure and algorithms!
What is a for
Loop?
Let’s start with the basics. Imagine you're hosting a Diwali party, and you’ve got a list of friends you need to invite. You wouldn’t call each friend individually if you had a simpler way to do it, right? In programming, a for
loop is like a tool that helps you send invitations to all your friends automatically, without the need to write separate code for each one.
In technical terms, a for
loop is a control flow statement that allows you to run a block of code a certain number of times. It’s particularly useful when you need to repeat an action multiple times, such as iterating over an array or performing a calculation repeatedly.
for
Loop Syntax
By now we know what a syntax is right? It is a certain defined rule which needs to be followed in a programming language.
Now, let's understand how to write a for
loop in Javascript:
for (initialization; condition; iteration) {
// Code to execute
}
Each for
loop consists of three key components:
- Initialization: This is where you set up a variable to start the loop. It’s like preparing your list of friends for the party.
- Condition: This determines how long the loop should run. As long as the condition is true, the loop continues. It’s like checking if you still have friends left to invite from your list!
- Iteration: This step updates the variable each time the loop runs. It’s like moving on to the next friend on your list after we invite the current one, got it?
Example of a Basic for
Loop
Let’s say you want to distribute sweets to a group of children in your neighborhood during Raksha Bandhan. You’ve got a box of 10 laddoos, and you want to give one to each child. Here’s how you can do it using a for
loop:
for (let i = 1; i <= 10; i++) {
console.log("Distributing laddoo to child number " + i);
}
Did you get it?
Here say there is a list of 10 children, hence the count is 10, we say start with the number 1, i.e. let i=1
, here we declare a variable on the fly to keep the count and initialize it to 1.
Let's understand it step by step:
In the above example:
- Initialization:
let i = 1;
- You start with the first child (i.e., child number 1). - Condition:
i <= 10;
- The loop runs as long as there are children left to receive laddoos (up to 10). - Iteration:
i++
- After giving a laddoo to one child, you move on to the next one by increasingi
by 1.
When you run this loop, you’ll see a message for each child, confirming that they’ve received their laddoo.
Let's take another example to help understand this better:
Imagine you’re at home, and your family is gathering for dinner. You’ve made a stack of rotis, but you want to count them to ensure everyone gets at least two. Here’s how you could use a for
loop to count the number of rotis:
let rotis = 12; // Assume you've made 12 rotis
for (let i = 1; i <= rotis; i++) {
console.log("Roti number " + i);
}
In this example, the loop runs 12 times, printing out a message for each roti. By the end of the loop, you’ve confirmed that you have 12 rotis ready to serve.
Nested for
Loops: Preparing Multiple Thalis
Sometimes, you need to use a loop within a loop. This is known as a nested for
loop. Imagine you’re preparing a traditional Indian thali (a complete meal served on a plate) for each family member. Each thali contains several items like roti, sabzi, dal, and rice. (Let me take a break and have one right now! Writing this is making me hungry) You want to prepare 5 thalis, and each thali has 3 rotis. Here’s how you can do it using nested for
loops:
for (let thali = 1; thali <= 5; thali++) {
console.log("Preparing thali number " + thali);
for (let roti = 1; roti <= 3; roti++) {
console.log(" Adding roti number " + roti + " to thali " + thali);
}
}
In this example:
- The outer loop (
thali
) runs 5 times, representing each thali. - The inner loop (
roti
) runs 3 times for each thali, adding 3 rotis to each one.
When you run this code, you’ll see a message for each thali and each roti added to that thali. It’s a great way to visualize how nested loops work in real life.

And so on...
Using Arrays with for
Loops: Shopping list
Let’s take a look at a practical example where you use a for
loop to iterate over an array. Imagine you’re preparing for a big family wedding, and you have a shopping list of items to buy. Here’s how you can loop through the shopping list and print out each item:
let shoppingList = ["Rice", "Dal", "Sugar", "Ghee", "Masalas"];
for (let i = 0; i < shoppingList.length; i++) {
console.log("Buying " + shoppingList[i]);
}
In this example:
- Initialization:
let i = 0;
- You start with the first item in the list (index 0). - Condition:
i < shoppingList.length;
- The loop runs as long as there are items left on the list. - Iteration:
i++
- After buying one item, you move on to the next one.
When you run this code, you’ll see a message for each item on your shopping list, confirming that you’re buying it.

Looping in Reverse
There might be times when you need to loop in reverse. Imagine you’re organizing a countdown for a Wedding ceremony, and you want to display a countdown from 10 to 1. Here’s how you can do it with a for
loop:
for (let i = 10; i > 0; i--) {
console.log("Wedding starts in " + i + " days");
}
console.log("Wedding has started!");
In this example:
- Initialization:
let i = 10;
- You start the countdown at 10. - Condition:
i > 0;
- The loop continues as long asi
is greater than 0. - Iteration:
i--
- After each second, you decreasei
by 1.
This loop prints a message for each day (or any unit that we need) of the countdown, and once the loop finishes, it announces that the Wedding has started.
There is more about how to use break and continue with loops as well, it deserves an article of its own, hence I would recommend reading it as well.
I hope you were able to understand for loops in detail, and ensure your foundation is solid with for
loops before proceeding to the next topic.