'Test if object contains property

I am trying to check for an object property, but I can't understand why the second option returns false. Could anyone explain? Also, are there any other better ways to check properties?

let question = {
    category: 'test'
}

console.log(question.hasOwnProperty('category')); // true

this wont work

let question = {
    category: 'test'
}

console.log(question.hasOwnProperty(question.category)); // false


Solution 1:[1]

const obj = {
    foo: 'bar',
};

// Check if an object has a certain key somewhere
console.log('foo' in obj);
console.log(!!obj['foo']);
console.log(obj.hasOwnProperty('foo'))

// Check if an object has a certain value somewhere
console.log(Object.values(obj).includes('bar'));

Solution 2:[2]

In this line:

question.hasOwnProperty(question.category)

The part question.category returns 'test' and you haven't 'test' like property, just leave it like that:

question.hasOwnProperty(category)

Solution 3:[3]

You can also check property using:

'category' in question

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mstephen19
Solution 2 lsanchezo
Solution 3 TheGoldyOne