🟒 EasyObjectsπŸ“– Theory Question

What are the different ways to enumerate object properties?

πŸ’‘

Hint

for...in, Object.keys, Object.values, Object.entries β€” differ in own vs inherited, enumerable vs all

Full Answer

Each enumeration method has different behavior around own properties, inherited properties, and enumerability:

const parent = { inherited: true };
const obj = Object.create(parent); // obj's prototype is parent
obj.a = 1;
obj.b = 2;
Object.defineProperty(obj, 'hidden', { value: 3, enumerable: false });

// for...in β€” own + inherited, enumerable only
for (const k in obj) console.log(k); // 'a', 'b', 'inherited'

// Object.keys β€” own properties, enumerable only ← most common
Object.keys(obj);    // ['a', 'b']

// Object.values β€” own, enumerable, values
Object.values(obj);  // [1, 2]

// Object.entries β€” own, enumerable, [key, value] pairs
Object.entries(obj); // [['a', 1], ['b', 2]]

// Object.getOwnPropertyNames β€” own, ALL (including non-enumerable)
Object.getOwnPropertyNames(obj); // ['a', 'b', 'hidden']

// Check if own property
obj.hasOwnProperty('a');         // true
obj.hasOwnProperty('inherited'); // false
Object.hasOwn(obj, 'a');         // ES2022 β€” preferred over hasOwnProperty
πŸ’‘ Use Object.keys/values/entries in modern code β€” they only return own enumerable properties. Use for...in only if you explicitly need inherited properties (rare).

More Objects Questions

🟒 EasyHow does prototypal inheritance work in JavaScript?β†’πŸŸ’ EasyWhat is the difference between shallow copy and deep copy?β†’πŸŸ‘ MediumWhat are property descriptors and property flags (writable, enumerable, configurable)?β†’πŸŸ’ EasyWhat are getters and setters in JavaScript?β†’

Practice this in a timed sprint β†’

5 free questions, no signup required

⚑ Start Sprint