0Pricing
JavaScript Academy · Lesson

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.

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
Loops: for, while, do…while, for…of vs for…in — illustration 1

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]);
}
Loops: for, while, do…while, for…of vs for…in — illustration 2

All lessons in this course

  1. if/else and switch — core patterns
  2. Loops: for, while, do…while, for…of vs for…in
  3. Early Returns and Guard Clauses
← Back to JavaScript Academy