← Lesson
BitWithBite
JavaScript · Quick Reference

Lesson 6 — Loops Cheat Sheet

JavaScript
In one line: Imagine you want to print the numbers 1 through 100. Without loops, you'd write console.log(1), console.log(2), all the way to console.log(100) — 100 separate lines. That's not ...

Key Ideas

1Why We Need Loops. Imagine you want to print the numbers 1 through 100. Without loops, you'd write console.log(1), console.log(2), all the way to console.log(100) — 100 separate lines. T...
2The for Loop. The for loop is the workhorse of JavaScript iteration. It packs initialization, condition, and update all into one concise line, making it easy to read when you know e...
3The while Loop. The while loop is the right choice when you don't know in advance how many iterations you need. You simply provide a condition, and the loop keeps running as long as t...
4The do...while Loop. The do...while loop is like a while loop with one crucial difference: it always executes the body at least once, then checks the condition. The condition check comes a...
5The for...of Loop. Introduced in ES6, for...of is the modern, clean way to iterate over iterable values — arrays, strings, Sets, Maps, and NodeLists. Instead of managing an index counter...

Code Examples

// Count from 1 to 5 for (let i = 1; i <= 5; i++) { console.log(`Count: ${i}`); } // Output: Count: 1, Count: 2, Count: 3, Count: 4, Count: 5 // Reverse countdown from 5 to 1 for (let i = 5; i >= 1; i--) { console.log(`T-minus ${i}...`); } consol...
// Simulating repeated input until valid entry let attempts = 0; let password = ""; while (password !== "secret123") { attempts++; // In a real app, this would prompt the user password = attempts === 3 ? "secret123" : "wrongpass"; console.log(`Atte...
// Menu system that always shows at least once let choice; let menuRuns = 0; do { menuRuns++; console.log("--- MENU ---"); console.log("1. Play Game"); console.log("2. View Scores"); console.log("3. Quit"); // Simulate user picking "Quit" on 2n...