Exercise 16: Get Object Size (key count)

Problem Statement

Function `objectSize(obj)` banao jo object me keys ki count return kare. Example: objectSize({a: 1, b: 2, c: 3}) => 3

Sample Output:

objectSize({a: 1, b: 2, c: 3}) => 3
objectSize({}) => 0

Solution

const objectSize = (obj) => {
  if (obj == null || typeof obj !== "object") return 0;
  return Object.keys(obj).length;
};

Explanation

Overall Goal:

  • Object me keys ki count return karna.

Real world:

  • Data analysis: object size.
  • Validation: non-empty checks.