JavaScript ECMAScript 2024 ·
✓ verified by execution on 2026-08-14
When you work with arrays, you have straightforward loops like for...of or .forEach(). But when you try to loop over a plain JavaScript object using for...of, you get a surprise.
Predict the outputjavascript
Read the code. What exactly will it print? Commit to an answer before you look.
const plainObj = { a: 1, b: 2 };try { for (const val of plainObj) { console.log(val); }} catch (e) { console.log(e.name + ": plainObj is not iterable");}
Output
TypeError: plainObj is not iterable
You predicted
Plain objects are not iterable. You cannot loop over them directly with for...of. To loop through an object in JavaScript, you must first convert it into an array of its keys, its values, or both.
The Object Iteration Methods
JavaScript provides three static methods on the Object constructor to extract arrays from an object.
Object.keys(obj) returns an array of the object’s own string-keyed property names.
Object.values(obj) returns an array of the object’s own string-keyed property values.
Object.entries(obj) returns an array of the object’s own string-keyed key-value pairs.
While for...in looks simpler than Object.keys(), there is a critical difference: for...in iterates over all enumerable string properties of an object, including inherited properties from the prototype chain. Object.keys() only returns the object’s own properties.
Because of this risk of accidentally looping over inherited methods or properties, modern JavaScript development heavily prefers Object.keys(), Object.values(), and Object.entries().
Iteration Order
A common misconception is that object properties are completely unordered. In modern JavaScript, iteration methods follow a specific, guaranteed order:
Integer keys (like '1', '2') are sorted in ascending numeric order.
String keys are returned in the chronological order they were inserted.
Symbol keys (which are ignored by Object.keys() and for...in) are returned last in insertion order.
Check yourself
What does `Object.values({ a: 1, b: 2 })` return?
Reveal answer
[ 1, 2 ] — Object.values() returns a standard Array containing only the object's values.
Why is `Object.keys()` generally preferred over `for...in` for plain objects?
Reveal answer
`Object.keys()` only returns the object's own properties, avoiding inherited ones. — `for...in` iterates over inherited enumerable properties on the prototype chain, which can cause unexpected bugs if the prototype has been modified.
Are JavaScript object properties completely unordered?
Reveal answer
No, modern JavaScript guarantees a specific order: integer keys first, then string keys by insertion. — Modern JavaScript engines follow a strict order: integer keys in ascending numeric order, followed by string keys in insertion order.
Challenges
🐞 Bug Hunt+25 XP
A developer tried to count the properties in `settings` but it prints `undefined` instead of `3`. Fix their code.
This code runs. It just does the wrong thing. Read it, find the defect,
fix it — the tests below decide when you are right.
javascript
Test 1 — expects "3\n"
Need a hint? (−25% XP)
Plain objects don't have a `.length` property. You need to extract an array of keys using `Object.keys()` first.
Your Object Is Not a Hash Map: Seven Expressions Most People Get Wrong — Use a Map instead of an object is advice everyone has heard and nobody follows, because the reasons are read as a list of properties. Predict what seven expressions evaluate to here, and the reasons stop being a list.