Loops in JavaScript (for, while, for-of, for-in)
Loops eliminate repetitive tasks by executing a block of code multiple times over arrays, numbers, or object properties.
What is it?
Loops are control structures that repeat instructions as long as a specified condition remains true.
Why is it used?
Used to iterate over lists of products, render cards, process API data, calculate averages, and filter search results.
Detailed Explanation
JavaScript Loop Types:
1. Standard `for` loop: `for (let i = 0; i < n; i++)` — perfect for numeric ranges and indexing.
2. `while` loop: Runs while condition is true. Check condition before running.
3. `do...while`: Guarantees code runs at least once before checking condition.
4. `for...of`: Modern ES6 loop for iterating over values of an Array or String.
5. `for...in`: Iterates over enumerable keys of an Object.
6. Loop Control: `break` exits the loop immediately; `continue` skips the rest of the current iteration and jumps to the next.
Syntax Structure
Syntax Blueprint
for (let i = 0; i < array.length; i++) { ... }
for (const item of array) { ... }
for (const key in object) { ... }
Code Example
JAVASCRIPT Code
const courses = ["HTML", "CSS", "JavaScript", "Python", "C", "C++"];
// 1. Modern for...of loop over array values
console.log("=== Course Catalog ===");
for (const course of courses) {
console.log(`• Course: ${course}`);
}
// 2. Standard for loop with break condition
console.log("=== First 3 Courses ===");
for (let i = 0; i < courses.length; i++) {
if (i >= 3) break; // Exit loop
console.log(`${i + 1}. ${courses[i]}`);
}
// 3. for...in loop over object keys
const instituteStats = {
students: 1250,
coursesOffered: 7,
rating: 4.9,
certified: true
};
console.log("=== Institute Stats ===");
for (const key in instituteStats) {
console.log(`${key}: ${instituteStats[key]}`);
}
Explanation of the Code
• `for...of` cleanly extracts each course string without manual index incrementing.
• `break` stops loop execution once 3 items are printed.
• `for...in` walks through each property key of the `instituteStats` object.
Expected Output
=== Course Catalog ===
• Course: HTML
• Course: CSS
• Course: JavaScript
• Course: Python
• Course: C
• Course: C++
=== First 3 Courses ===
1. HTML
2. CSS
3. JavaScript
=== Institute Stats ===
students: 1250
coursesOffered: 7
rating: 4.9
certified: true
Real-World Application
Used across frontend rendering to display card lists, generate table rows, and process batches of records.
Common Mistakes to Avoid
• Creating an accidental infinite loop by forgetting to increment the counter in a `while` loop, crashing the browser tab.
• Using `for...in` on arrays (it iterates over indices as strings rather than array values; use `for...of` for arrays).
Quick Practice Challenge
Write a loop that calculates the sum of all numbers in `[10, 20, 30, 40, 50]` using `for...of`.
Key Takeaways
• Use `for...of` to iterate over array items.
• Use `for...in` to iterate over object keys.
• Use `break` to exit loops early and `continue` to skip an iteration.