hasProperty

Easy · Object Manipulation · 2 test cases

Write a function that checks if an object has a given property.

Examples

hasProperty({a: 1, b: 2}, 'a') → true
hasProperty({a: 1, b: 2}, 'c') → false

Starter code

function hasProperty(obj, prop) {

}

Complexity of the optimal solution

Time O(1), space O(1). Checking for property existence in an object using `hasOwnProperty` or the `in` operator is typically a constant time operation on average, due to the hash map implementation of objects.

Solve hasProperty in the browser