Recall & Review
beginner
What is the simplest way to get all keys from a JavaScript object?
Use
Object.keys(object). It returns an array of all the object's own keys.Click to reveal answer
beginner
How do you loop over all key-value pairs in an object using
for...in?Use <code>for (const key in object) { const value = object[key]; }</code>. It loops over all enumerable keys, including inherited ones.Click to reveal answer
beginner
What method returns an array of an object's own enumerable property [key, value] pairs?
Use
Object.entries(object). It returns an array of arrays, each with [key, value].Click to reveal answer
intermediate
Why should you use
Object.hasOwn(object, key) inside a for...in loop?To check if the key belongs directly to the object and not inherited from its prototype chain.
Click to reveal answer
beginner
How can you iterate over an object’s keys and values using
Object.entries and for...of?Use <code>for (const [key, value] of Object.entries(object)) { /* use key and value */ }</code> for clean and direct access.Click to reveal answer
Which method returns an array of an object's keys?
✗ Incorrect
Object.keys() returns an array of the object's own keys.
What does
for...in loop iterate over?✗ Incorrect
for...in loops over all enumerable keys, including inherited keys.Which method gives you both keys and values as pairs in an array?
✗ Incorrect
Object.entries() returns an array of [key, value] pairs.
Why use
Object.hasOwn(object, key) inside a for...in loop?✗ Incorrect
It ensures you only process keys that belong directly to the object.
Which loop works best with
Object.entries() for clean key-value access?✗ Incorrect
for...of loops over arrays like the one returned by Object.entries().Explain how to iterate over all keys and values of a JavaScript object safely.
Think about methods that return keys or entries and how to avoid inherited keys.
You got /4 concepts.
Describe the difference between
for...in and for...of when iterating over objects.Consider what each loop expects to work with and what they return.
You got /4 concepts.