3 ways to check if a Javascript object property exists

Take a basic example to start with:

const foo = {
    bar: "baz"
};

Here are three ways to check if the object foo has a property called bar

hasOwnProperty

For the example above

foo.hasOwnProperty('bar');    // true
foo.hasOwnProperty('boo');    // false

in operator

For the example above:

return ('bar' in foo) ? true : false;

Compare with undefined

For the example above:

return (foo.bar !== undefined) ? true : false;