Skip to content
dreamcode
dreamcode
Map
Advanced loops
Lesson 21 of 48
+15 XP on finish
JAVASCRIPT CLIMBSChapter 4 · JS Collections Depth

For Of and For In

JavaScript provides loops tailored to specific data shapes: for...of (loops through array elements directly) and for...in (loops through the keys of an object).

Worked example

How it reads

  • for (const star of stars) iterates over the elements themselves
  • for (const key in config) iterates over the object's keys
Cloud tip: Never use for...in to loop over arrays; it iterates over index strings, which can lead to unexpected type conversion errors.
index.js
JAVASCRIPT
real JavaScript, runs in your browser
Console
Run your code to see its output here.
YOUR TURN

Add a for...in loop over const sizes = { cirrus: 3, cumulus: 7 } that logs lines like cirrus 3 after the clouds.

Press Run to check your work.