What it is
In JavaScript, a plain object works as a dictionary: it maps keys to values. A loop can visit every entry, one at a time, and the body runs once per key. This is how you add up all the values, count the entries that meet a rule, or print everything.
Two ways to loop
for...in gives the keys of an object. Inside the loop, square brackets read the value of the current key:
const stock = {"apples": 5, "pears": 2};
for (const fruit in stock) {
console.log(fruit, stock[fruit]);
}
apples 5
pears 2
To get each key together with its value, loop over Object.entries(stock). Each entry is a [key, value] pair, and the loop can unpack it right in the header:
for (const [fruit, amount] of Object.entries(stock)) {
console.log(fruit, amount);
}
It prints the same two lines. Object.keys(stock) gives just the keys as an array, [ 'apples', 'pears' ], and Object.values(stock) gives just the values, [ 5, 2 ].
The order
An object keeps text keys in the order they were added, so apples comes before pears above. Many other languages make no such promise, so write code that gives the same answer in any order. A total or a count does. Be careful with keys that look like whole numbers: JavaScript visits those first, in ascending order, before the other keys. For {b: 1, a: 2, 10: "x", 2: "y"} the loop visits 2, 10, b, a.
Traps
for...ofstraight on an object fails. An object is not iterable, sofor (const x of stock)throws the error "stock is not iterable". Usefor...in, orfor...ofoverObject.entries(stock).ingives keys, not values. Withfor (const v in scores), addingvto a number glues text together: a total that starts at 0 turns into0AnaBenCleoDan.- Dot access names a literal key. Inside the loop,
scores.namelooks for a key calledname, which doesn't exist, so it isundefinedand a total built from it ends asNaN. Usescores[name]to read the key held in the variable.
The exercise, step by step
A game keeps each player's points in scores. The variables total and count start at 0. The task is to add every player's points to total and to count the players with 10 points or more.
- Write a
for...inloop overscoreswith aconstnamednamefor the key, and close its brace right away. - Inside it, add
scores[name]tototal. - Below that, if
scores[name]is 10 or more, add 1 tocount.
const scores = {"Ana": 12, "Ben": 7, "Cleo": 15, "Dan": 9};
let total = 0;
let count = 0;
for (const name in scores) {
total += scores[name];
if (scores[name] >= 10) {
count += 1;
}
}
console.log(total);
console.log(count);
The output is:
43
2
The total is 12 + 7 + 15 + 9 = 43, and Ana and Cleo are the two players with 10 or more.
The same exercise with entries
The loop with Object.entries gives the same result, and it reads the value only once per player. The variable points holds the value directly, so the loop body never writes scores[name]:
for (const [name, points] of Object.entries(scores)) {
total += points;
if (points >= 10) {
count += 1;
}
}
It also prints 43 and 2. Either form is fine; the lesson's tests only look at the printed output.
Key takeaways
for (const key in obj)visits each key; read the value withobj[key].Object.entries(obj)gives[key, value]pairs forfor...of.- A plain object is not iterable, so
for...ofon it directly is an error. - Totals and counts don't depend on the order of the keys.
