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: true
console.log(hasProperty({ city: "New York", population: 8419000 }, "country")); // Expected output: false
console.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.

window code 2Test Cases

Input:

{
  "city": "New York",
  "population": 8419000
}

Expected Output:

false