Learn JavaScript FOR LOOPS in 5 minutes! (5:45)
For loops repeat some code a limited amount of times. Unlike while loops which can repeat code infinitely, for loops allow you to do something a certain or limited number of times. Learn counter initialization, conditions, increment/decrement operations, and the continue/break keywords.
- For loop syntax and structure
- Counter initialization
- Increment and decrement
- Loop conditions
- Counting up and down
- Stepping by different amounts
- When to use for vs while loops
for (initialization; condition; increment/decrement) {
// Code to repeat
}Three parts:
- Initialization - Set starting value (runs once)
- Condition - Continue while true (checked each iteration)
- Increment/Decrement - Update counter (after each iteration)
for (let i = 0; i <= 2; i++) {
console.log("Hello");
}
// Output: Hello (3 times)Understanding the three statements:
let i = 0- Create temporary counter, start at 0i <= 2- Continue while i is less than or equal to 2 (0, 1, 2 = 3 times)i++- Increment counter by 1 after each iteration
for (let i = 0; i <= 2; i++) {
console.log(i);
}
// Output: 0, 1, 2for (let i = 1; i <= 10; i++) {
console.log(i);
}
// Output: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10for (let i = 0; i < 10; i++) {
console.log(i);
}
// Output: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9for (let i = 1; i <= 10; i += 2) {
console.log(i);
}
// Output: 1, 3, 5, 7, 9Start at 2 for even numbers:
for (let i = 2; i <= 10; i += 2) {
console.log(i);
}
// Output: 2, 4, 6, 8, 10for (let i = 10; i > 0; i--) {
console.log(i);
}
console.log("HAPPY NEW YEAR!");
// Output: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, HAPPY NEW YEAR!It's like counting down to midnight on New Year's Eve!
for (let i = 10; i > 0; i -= 2) {
console.log(i);
}
console.log("HAPPY NEW YEAR!");
// Output: 10, 8, 6, 4, 2, HAPPY NEW YEAR!for (let i = 10; i >= 1; i -= 3) {
console.log(i);
}
console.log("HAPPY NEW YEAR!");
// Output: 10, 7, 4, 1, HAPPY NEW YEAR!for (let i = 1; i <= 20; i++) {
if (i == 13) {
continue; // skip iteration when i == 13
}
else {
console.log(i);
}
}
// Output: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20What happens:
- Loop counts from 1 to 20
- When
iequals 13 (unlucky number!),continueis executed continueskips the rest of the current iteration- Jumps from 12 directly to 14
- Notice 13 is missing from the output
Use continue when: You need to skip an iteration based on a condition
for (let i = 1; i <= 20; i++) {
if (i == 13) {
break; // exit loop entirely when i == 13
}
console.log(i);
}
// Output: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12What happens:
- Loop counts from 1 to 20
- When
iequals 13,breakis executed breakexits the for loop entirely- No more iterations happen
- We count up to 12, then stop completely
Use break when: You need to exit the loop early before all iterations complete
| Keyword | Action | Use Case |
|---|---|---|
continue |
Skip current iteration, continue loop | Skip specific values |
break |
Exit loop entirely | Stop when condition met |
Recap from Switches:
- We've seen
breakbefore in switch statements to break out of the switch - Same concept here -
breakexits the for loop entirely
let number = 5;
for (let i = 1; i <= 10; i++) {
console.log(`${number} x ${i} = ${number * i}`);
}
// Output:
// 5 x 1 = 5
// 5 x 2 = 10
// ... up to 5 x 10 = 50let sum = 0;
for (let i = 1; i <= 100; i++) {
sum += i;
}
console.log(`Sum of 1 to 100: ${sum}`);
// Output: Sum of 1 to 100: 5050for (let i = 1; i <= 10; i++) {
if (i % 2 === 0) {
continue; // Skip even numbers
}
console.log(i);
}
// Output: 1, 3, 5, 7, 9 (odd numbers only)let numbers = [3, 7, 2, 9, 4, 8, 1];
let target = 9;
for (let i = 0; i < numbers.length; i++) {
console.log(`Checking ${numbers[i]}...`);
if (numbers[i] === target) {
console.log(`Found ${target} at index ${i}!`);
break; // Stop searching once found
}
}
// Output:
// Checking 3...
// Checking 7...
// Checking 2...
// Checking 9...
// Found 9 at index 3!for (let i = 1; i <= 10; i++) {
if (i === 5) {
break; // Stop at 5
}
console.log(i);
}
// Output: 1, 2, 3, 4for (let i = 1; i <= 10; i++) {
if (i === 5) {
continue; // Skip 5
}
console.log(i);
}
// Output: 1, 2, 3, 4, 6, 7, 8, 9, 10// Calculate sum of all even numbers from 1 to 100let str = "Hello";
// Use for loop to print string in reverse// Print all prime numbers from 1 to 50let numbers = [10, 20, 30, 40, 50];
// Calculate sum using for loop// Print:
// *
// ***
// *****
// *******
// *********for (let i = 0; i < 5; i++) {
console.log(i);
}let fruits = ["apple", "banana", "cherry"];
for (let fruit of fruits) {
console.log(fruit);
}let person = {name: "John", age: 30, city: "NYC"};
for (let key in person) {
console.log(`${key}: ${person[key]}`);
}- ✅ Know exact number of iterations
- ✅ Counting/iterating with counter
- ✅ Going through arrays (with index)
- ✅ Defined start and end
// ✅ Good use of for loop
for (let i = 0; i < 10; i++) {
console.log(i);
}- ✅ Unknown number of iterations
- ✅ Condition-based looping
- ✅ User input validation
- ✅ Loop until specific event
// ✅ Good use of while loop
let password = "";
while (password.length < 8) {
password = prompt("Enter password:");
}// ❌ Infinite loop
for (let i = 0; i < 10; ) { // Forgot i++
console.log(i);
}
// ✅ Correct
for (let i = 0; i < 10; i++) {
console.log(i);
}// ❌ Runs 11 times (0-10)
for (let i = 0; i <= 10; i++) { }
// ✅ Runs 10 times (0-9)
for (let i = 0; i < 10; i++) { }// ❌ Infinite loop (counting up forever)
for (let i = 10; i > 0; i++) { }
// ✅ Correct countdown
for (let i = 10; i > 0; i--) { }// ❌ Unclear
for (let i = 0; i < users.length; i++) { }
// ✅ Better
for (let userIndex = 0; userIndex < users.length; userIndex++) { }// ❌ Magic number
for (let i = 0; i < 100; i++) { }
// ✅ Named constant
const MAX_ITEMS = 100;
for (let i = 0; i < MAX_ITEMS; i++) { }- For loops repeat code a limited number of times (unlike while loops)
- Three statements:
- Create temporary counter variable (e.g.,
let i = 0) - Condition to continue (
i < 10) - Increment or decrement counter (
i++ori--)
- Create temporary counter variable (e.g.,
- i is a common naming convention meaning "index"
- i++ increments by 1, i-- decrements by 1
- i += n increments by n, i -= n decrements by n
- continue skips current iteration, continues with next
- break exits the loop entirely (also seen in switch statements)
- Perfect for when you know exactly how many times to iterate
- Common counter pattern: start at 0 or 1, increment by 1 each time
- Watch for off-by-one errors (< vs <=)
JavaScript NUMBER GUESSING GAME
Duration: 5:45
Difficulty: Beginner
Category: Loops & Iteration