Easy
Check if a Property Exists in an Object
Description
In JavaScript, objects store data as key-value pairs. Sometimes, you may need to check if a specific property (key) exists in an object.
In this challenge, you will write a function named hasProperty that takes an object and a property name (key) as input and returns true if the property exists in the object, and false otherwise.
Example Usage:
console.log(hasProperty({ name: "Alice", age: 25 }, "age")); // Expected output: trueconsole.log(hasProperty({ city: "New York", population: 8419000 }, "country")); // Expected output: falseconsole.log(hasProperty({}, "name")); // Expected output: false
Constraints:
- The input will always be a valid object and a string representing the property name.
- The function should return true if the property exists in the object, and false otherwise.
Did you know?
www stands for World Wide Web.
Test Cases
Input:
{ "city": "New York", "population": 8419000 }
Expected Output:
false