Easy
Count the Number of Properties in an Object
Description
n JavaScript, objects are used to store collections of key-value pairs. Each key-value pair is called a property.
Sometimes, you may need to count how many properties an object has.
In this challenge, you will write a function named countProperties that takes an object as input and returns the number of properties it contains.
Example Usage:
console.log(countProperties({ name: "Alice", age: 25 })); // Expected output: 2console.log(countProperties({ city: "New York", population: 8419000, country: "USA" })); // Expected output: 3console.log(countProperties({})); // Expected output: 0
Constraints:
- The input will always be a valid object.
- The function should return the number of properties in the object.
- If the object is empty, the function should return 0.
Did you know?
JavaScript was originally called "LiveScript" before being renamed to JavaScript.
Test Cases
Input:
{ "age": 25, "name": "Alice" }
Expected Output:
2