0
0
Javascriptprogramming~5 mins

Iterating over objects in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AObject.keys()
BObject.values()
CObject.entries()
Dfor...in loop
What does for...in loop iterate over?
AOnly own enumerable keys
BOnly values of the object
COnly symbols
DAll enumerable keys including inherited ones
Which method gives you both keys and values as pairs in an array?
AObject.entries()
BObject.values()
CObject.keys()
Dfor...of loop
Why use Object.hasOwn(object, key) inside a for...in loop?
ATo convert object to array
BTo get values
CTo skip inherited keys
DTo get keys
Which loop works best with Object.entries() for clean key-value access?
Afor...in
Bfor...of
Cwhile
Ddo...while
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.