Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Count Keys

Data Structures, Operators & Stringseasy

Course · Section 9: Data Structures, Modern Operators and Strings · Lecture 118: Enhanced Object Literals

Implement `countKeys(obj)` returning the number of own enumerable keys.

Sample tests

Input: countKeys({"a":1,"b":2})
Output: 2
Input: countKeys({})
Output: 0

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Inherited or non-enumerable keys are not counted by Object.keys, which is usually what you want.

Learning resources

  • MDN: Object
Approach & explanation (try first)

Object.keys(obj).length counts the object's own enumerable properties.

Loading...
⌘/Ctrl + Enter

Run your code to see results.