Sub topic 2
for...in: Walking an Object's Property Keys
This chapter assumes you know how to declare variables with let and const, and how to read object properties with dot and bracket notation. It covers the basic shape of a for...in loop and what the loop variable holds. Later chapters cover which properties the loop skips, inherited keys and key order.
The Basic Shape
A for...in loop runs its body once for each property key of an object. On each pass, the loop variable holds the name of one property.
const user = { name: 'Ada', role: 'admin' };
for (const key in user) {
console.log(key, user[key]);
}
// name Ada
// role adminThe loop head has three parts:
- A declaration (
const key). This names the variable that receives each key. - The keyword
in. - An expression that evaluates to an object (
user).
The loop gives you keys and never values. To get a value, you look it up yourself with user[key].
Keys Are Always Strings
Every key that for...in hands you is a string, including keys that look like numbers.
const stock = { 101: 'bolts', 102: 'nuts' };
for (const id in stock) {
console.log(typeof id, id + 1);
}
// string 1011
// string 1021id + 1 gives '1011' instead of 102 because + joins two strings. When you need a number, convert it explicitly with Number(id).
Properties keyed by a Symbol never appear in for...in. The loop only visits string keys.
Common Mistake: Dot Access with the Loop Variable
The most common bug is writing obj.key instead of obj[key]:
const user = { name: 'Ada', role: 'admin' };
for (const key in user) {
console.log(user.key); // undefined, undefined
}user.key looks up a property literally called "key", and that property does not exist. Bracket notation evaluates the variable and uses its contents as the property name.
Declaring the Loop Variable
You can use const, let, var, or a variable that already exists:
| Form | Notes |
|---|---|
| `for (const k in obj)` | Preferred. You get a new binding on each pass, and the key can't be reassigned by accident. |
| `for (let k in obj)` | Use this if the body needs to reassign `k`. |
| `for (var k in obj)` | Function-scoped. `k` remains visible after the loop. |
| `for (k in obj)` | Assigns to an existing variable. If none exists, it creates an implicit global in sloppy mode or throws in strict mode. |
const is allowed here even though the variable changes between passes. Each pass creates a new binding instead of reassigning the old one. A later chapter covers why this matters for closures.
Exercise
Given const prices = { 3: 9.5, 7: 4 };, what does for (const k in prices) console.log(k * 2, k + 2); print, and why do the two expressions differ?
Show answer
It prints 6 '32' and then 14 '72' (shown as 6 32 and 14 72 in most consoles). The key k is a string. * converts it to a number, but + with a string operand joins the two as strings.
Exercise
Fix this loop so it prints each value: for (const field in config) { console.log(config.field); }
Show answer
Use bracket notation: console.log(config[field]);. config.field reads a property literally named "field" instead of using the variable's contents.