Lesson 40 of 48
83%
For In Loop
for...in walks through the keys of an object.
Each round gives you one key name.
Think of opening labeled drawers one by one.
1
Loop object keys
2
Read values with keys
3
Care with arrays
4
hasOwnProperty check
Basic for...in
Write for (const key in object).
javascript
Get the values
Use the key inside brackets to read the value.
javascript
Why not for arrays
for...in can give index strings and extra inherited keys.
For arrays, for...of or a normal for loop is clearer.
javascript
Own keys only
If you worry about inherited keys, check hasOwnProperty.
javascript
Object.keys alternative
Many people turn keys into an array first.
Then they use for...of.
javascript
Tip: Use for...in for object keys. Use for...of for array values.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.