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. This is lesson 2 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 3 lessons in total.
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.

Learn JavaScript with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 59
- Lessons
- 200
Frequently Asked Questions
Is the “Loops: for, while, do…while, for…of vs for…in” lesson free?
Yes — any 3 lessons of the JavaScript Academy learning path, including “Loops: for, while, do…while, for…of vs for…in”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 3 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 3.
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