Lesson 8: JavaScript Looping
Master JavaScript looping constructs with interactive examples and real-world applications.
8.1 Introduction to Looping
Looping allows you to execute a block of code repeatedly until a specified condition is met. JavaScript provides several looping mechanisms:
Initialization
Set starting point
Condition Check
Is condition true?
Execute Code
Run loop body
Update
Modify condition
8.2 The For Loop
The for loop is the most common looping structure in JavaScript. It consists of three parts:
for (initialization; condition; update) { // Code to execute each iteration }
Example 8.1: Sum Numbers
let sum = 0; for (let i = 1; i <= 10; i++) { sum += i; } document.write(`Sum of 1 to 10: ${sum}`); // Output: 55
Interactive For Loop Demo
8.3 For...In Loop
The for...in loop iterates over the properties of an object. It's useful for working with object properties.
for (let key in object) { // Code to execute for each property }
Example 8.2: Object Properties
const student = { name: 'John', age: 21, course: 'Computer Science' }; for (let prop in student) { document.write(`${prop}: ${student[prop]}<br>`); } // Output: // name: John // age: 21 // course: Computer Science
Interactive For...In Loop Demo
8.4 While Loop
The while loop executes a block of code as long as a specified condition is true.
while (condition) { // Code to execute }
Example 8.3: Countdown
let count = 5; while (count > 0) { document.write(`${count}...<br>`); count--; } document.write("Liftoff!");
Interactive While Loop Demo
8.5 Do...While Loop
The do...while loop is similar to the while loop but guarantees at least one execution of the loop body.
do { // Code to execute } while (condition);
Example 8.4: Input Validation
let userInput; do { userInput = prompt("Enter a number greater than 100:"); } while (userInput <= 100); document.write(`Thank you! You entered: ${userInput}`);
Interactive Do...While Demo
8.6 Loop Control Statements
JavaScript provides two important statements for controlling loop execution:
break
Immediately terminates the loop
continue
Skips to the next iteration
Example 8.5: Using break and continue
for (let i = 1; i <= 10; i++) { if (i === 5) continue; // Skip 5 if (i === 8) break; // Stop at 8 document.write(i + "<br>"); } // Output: 1, 2, 3, 4, 6, 7
Break/Continue Demo
8.7 Real-World Applications
Loops are fundamental to many programming tasks. Here are some practical examples:
Example 8.6: Processing Arrays
const products = [ { name: 'Laptop', price: 999 }, { name: 'Phone', price: 699 }, { name: 'Tablet', price: 399 } ]; // Calculate total price let total = 0; for (let i = 0; i < products.length; i++) { total += products[i].price; } document.write(`Total: $${total}`); // Output: Total: $2097
Example 8.7: Generating HTML Elements
const colors = ['red', 'green', 'blue', 'yellow']; let html = '<div class="color-grid">'; for (let color of colors) { html += `<div style="background:${color}">${color}</div>`; } html += '</div>'; document.body.innerHTML += html;