TetSharing
JavaScript tutorial · 19 chapters
  1. Get started
  2. let, const and var
  3. Data types
  4. Operators and equality
  5. Strings
  6. Template literals
  7. if, else and switch
  8. Loops
  9. Functions
  10. Arrow functions
  11. Arrays
  12. map, filter, reduce
  13. Objects
  14. Destructuring and spread
  15. Classes
  16. Errors and try...catch
  17. Promises
  18. async and await
  19. JSON

Final JavaScript quiz
Cheat sheet: javascript array methods

Chapter 8 of 19 · By Ethan Cole · Updated November 21, 2026

JavaScript Loops: for, for...of, for...in and while

JavaScript has several loops, and picking the right one makes code clearer. In everyday code you will mostly use for...of for arrays and the classic counting for when you need an index.

The classic for loop

for (let i = 0; i < 3; i++) has three parts separated by semicolons: an initializer that runs once, a condition checked before each pass, and an update that runs after each pass. It is flexible: you can count down, step by two, or stop early. Declare the counter with let so it stays inside the loop.

for...of: values of an iterable

for (const lang of langs) gives you each value of an array, one at a time, without any index bookkeeping. It works on anything iterable: arrays, strings (character by character), Map, Set and more. Use const for the loop variable unless you reassign it inside the body.

for...in: keys of an object

for (const key in book) gives you the property names of an object as strings. Read the value with book[key]. Do not use for...in on arrays: it yields indexes as strings, can include inherited properties, and is easy to confuse with for...of. For objects, Object.entries() combined with for...of is a popular alternative, as you will see in the objects chapter.

JavaScript · editable

Expected output

i is 0
i is 1
i is 2
JS
SQL
title = Maps of Memory
year = 2024
first power of 3 over 50: 81

while and do...while

while (condition) repeats as long as the condition is truthy, which suits loops where you do not know the number of passes in advance. In the example it multiplies by 3 until the value passes 50. A do { } while (condition) loop runs the body once before checking, which guarantees at least one pass.

break and continue

break leaves the loop immediately; continue skips to the next pass. The for...of loop above uses continue to skip "Python". Both work in every loop type.

Common mistakes

  • Using for...in on an array when you meant for...of.
  • Off-by-one conditions: i <= arr.length reads one item past the end and gets undefined.
  • Infinite while loops because nothing in the body changes the condition.

Summary

  • Classic for (init; condition; update) for counting and indexes.
  • for...of for values of arrays, strings, Maps and Sets.
  • for...in for the keys of plain objects.
  • while when the number of passes is unknown; break and continue control flow.

Quiz: Loops

1. What does for (const ch of "hi") give ch?

Strings are iterable, so for...of yields each character.

2. What does for...in iterate over in an object?

for...in yields the enumerable property names as strings.

3. How many times does for (let i = 0; i < 5; i += 2) run its body?

i takes the values 0, 2 and 4.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.