Loops: for, while, do…while, for…of vs for…in
Loop basics: for, while, do…while, and when to use for…of vs for…in for arrays and objects.
Loops: for, while, do…while, for…of vs for…in is a free JavaScript Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Intro
Goal: Pick the right loop. Learn for, while, do…while, and when to use for…of vs for…in.
- Counted loops
- Condition loops
- Iterating values vs keys

for loop basics
for is ideal when you need the index and a stopping condition.
// Classic for: great for indexes
const nums = [2, 4, 6];
for (let i = 0; i < nums.length; i++) {
console.log("Index:", i, "Value:", nums[i]);
}

while loop basics
while checks the condition first; be sure the loop variable changes.
// while: run while a condition stays true
let count = 3;
while (count > 0) {
console.log("Countdown:", count);
count = count - 1;
}
console.log("Go!");

do…while basics
do…while runs the body first, then checks; useful when you need one guaranteed run.
// do…while: runs the body at least once
let tries = 0;
do {
console.log("Attempt:", tries + 1);
tries = tries + 1;
} while (tries < 2);

for…of vs for…in
for…of yields values; for…in yields keys and can include inherited keys.
// for…of: values of an iterable (arrays, strings, etc.)
// for…in: keys (property names)
const colors = ["red", "green", "blue"];
for (const value of colors) {
console.log("of value:", value);
}
for (const key in colors) {
console.log("in key:", key);
}

Loop tips
Tips:
- Prefer for…of for arrays to get values directly.
- Use for…in for object keys (not arrays).
- Avoid infinite loops: always update your loop variable.

for…of vs for…in quiz
Quick check: Array values directly.

Recap
Recap: Use for for indexes, while for condition-led loops, do…while to run once then check, and for…of for array values.

Frequently asked questions
Is the “Loops: for, while, do…while, for…of vs for…in” lesson free?
Yes — the full text of “Loops: for, while, do…while, for…of vs for…in” is free to read here on the web, and the JavaScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Loops: for, while, do…while, for…of vs for…in”?
Loop basics: for, while, do…while, and when to use for…of vs for…in for arrays and objects. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start JavaScript Academy?
No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Loops: for, while, do…while, for…of vs for…in” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this JavaScript Academy lesson?
Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- if/else and switch — core patterns
- Loops: for, while, do…while, for…of vs for…in
- Early Returns and Guard Clauses