JavaScript Loops
Master loops: learn how to use for, while, and do-while to repeat code efficiently. Control iteration with break and continue statements. Complete guide with real-world examples.
In This Lesson
- What are Loops
- for Loop
- while Loop
- do-while Loop
- break & continue
- for...in Loop
- for...of Loop
- Best Practices
- FAQ
What are JavaScript Loops?
Loops allow you to repeat code multiple times without rewriting it. They're essential for processing data, iterating through arrays, and automating repetitive tasks. JavaScript provides several types of loops, each suited for different scenarios.
Why use loops?
The for Loop
The for loop is used to repeat code a specific number of times. It's the most common loop and works great when you know exactly how many iterations you need.
Syntax & Structure
for (initialization; condition; increment) {
// code to be executed
}
Basic Examples
// Loop 5 times for (let i = 0; i < 5; i++) { // Runs with i: 0, 1, 2, 3, 4 } // Count down for (let i = 10; i >= 1; i--) { // Counts from 10 down to 1 }
Iterating Over Arrays
let fruits = ["Apple", "Banana", "Orange"];
for (let i = 0; i < fruits.length; i++) {
// fruits[0], fruits[1], fruits[2]
}
The while Loop
The while loop repeats code as long as a condition is true. Use it when you don't know exactly how many iterations you need.
Syntax
while (condition) {
// code to be executed
// Must change condition to avoid infinite loop!
}
Examples
let i = 0;
while (i < 5) {
// Runs 5 times
i++;
}
// User input example
let input = "";
while (input !== "quit") {
input = prompt("Type 'quit' to exit:");
}
The do-while Loop
The do-while loop is like while, but it executes the code block at least once before checking the condition.
Syntax
do {
// code to be executed
} while (condition);
Examples
let i = 0;
do {
// Runs at least once
i++;
} while (i < 5);
// Menu system
let choice = "";
do {
choice = prompt("1: Start 2: Settings 3: Exit");
} while (choice !== "3");
break & continue Statements
Control loop execution flow with break (exit immediately) and continue (skip to next iteration).
break Statement
Terminates the loop immediately and jumps to code after the loop.
for (let i = 0; i < 10; i++) {
if (i === 5) {
break; // Exits loop immediately
}
// Runs for i: 0, 1, 2, 3, 4
}
continue Statement
Skips the current iteration and moves to the next one.
for (let i = 0; i < 5; i++) {
if (i === 2) {
continue; // Skips this iteration
}
// Runs for i: 0, 1, 3, 4 (skips 2)
}
Comparison: break vs continue
The for...in Loop
The for...in loop iterates over object properties and array indices. Perfect for objects with named properties.
Syntax
for (key in object) {
// code to be executed
}
Examples
let person = {
name: "John",
age: 30,
city: "New York"
};
for (let key in person) {
// key: "name", "age", "city"
// person[key]: "John", 30, "New York"
}
// With arrays (not ideal)
let fruits = ["Apple", "Banana"];
for (let index in fruits) {
// index: "0", "1" (as strings!)
}
The for...of Loop
The for...of loop iterates over values of iterables (arrays, strings). It's cleaner than for...in for arrays and strings.
Syntax
for (value of iterable) {
// code to be executed
}
Examples
let fruits = ["Apple", "Banana", "Orange"];
for (let fruit of fruits) {
// fruit: "Apple", "Banana", "Orange"
}
// With strings
let text = "Hello";
for (let char of text) {
// char: "H", "e", "l", "l", "o"
}
Loop Type Comparison
Best Practices
Frequently Asked Questions
Common questions about JavaScript loops:
Loops allow you to repeat code multiple times without rewriting it. They're essential for processing arrays, iterating through data, and automating repetitive tasks in JavaScript.
A while loop checks the condition first, so it might not run at all. A do-while loop executes the code at least once before checking the condition. Use do-while for menus or input validation.
Use a for loop when you know exactly how many times you need to repeat code. It's perfect for iterating through arrays with a known length and counting up or down.
The break statement immediately exits the loop without executing remaining iterations. It's useful when you find what you're looking for and don't need to continue searching.
The continue statement skips the current iteration and moves to the next one. It's useful for ignoring certain items while processing the rest of your data.
Use for...of for arrays because it gives you the actual values directly. for...in returns indices as strings and isn't designed for arrays. However, for...in is great for objects.