JS Built-ins
← Object100 / 150
ObjectObject.keysbeginner

Object.keys() β€” counting and iterating

πŸ“– Usage Example
// Get array of own enumerable property names
const obj = { a: 1, b: 2, c: 3 }
Object.keys(obj)   // β†’ ['a', 'b', 'c']

Use Object.keys() to get an array of an object's own enumerable property names for iteration or counting.

Object.keys() β€” practical use

Object.keys() is commonly used for iterating over an object's properties or counting them.

Challenge: Implement countProperties(obj) that returns the number of own enumerable properties.

πŸ’‘ Hints (1)
  • β€’ Object.keys().length is idiomatic for counting object properties