- Home
- /
- For Loops
JavaScript Tutorial
IINTRODUCTION
JAVASCRIPT VARIABLES
JAVASCRIPT BASICS
JAVASCRIPT OBJECTS
DOM & BOM
OOPs
- Home
- /
- For Loops
For Loops
In programming, loops are used to run the same block of code again and again. JavaScript gives us three main types of for
loops — each built for a different situation. Let’s explore all three in a simple, human-friendly way.
Standard for Loop
This is the classic loop you’ve probably seen in every beginner’s course. It’s perfect when you know exactly how many times you want to repeat something.
Structure:
for (start; condition; update) {
// code runs here
}
Example:
for (let count = 1; count <= 5; count++) {
console.log("Counting:", count);
}
This loop starts at 1 and runs until count
is 5. After each round, it increases by 1.
for...in Loop
Use this loop when you want to go through the keys (property names) of an object.
Example:
let student = {
name: "Areeba",
age: 18,
grade: "A+"
};
for (let key in student) {
console.log(key + " ➤ " + student[key]);
}
Output:
name ➤ Areeba
age ➤ 18
grade ➤ A+
Great for looping through object properties. But don’t use it for arrays — it’s not meant for that.
for...of Loop
When you’re working with arrays, strings, or anything iterable, this loop is your best friend. It gives you the values, not keys.
Example:
let fruits = ["Mango", "Banana", "Peach"];
for (let fruit of fruits) {
console.log("I love", fruit);
}
Output:
I love Mango
I love Banana
I love Peach
Perfect when you only care about the items, not their index.