Suppose I have this:
var person = { "name": "John Doe", "email": "john.doe@example.com" };
This object only has two elements called name and email. Some persons also have an element age, but this particular person doesn't. What's the best way to check this?
if (person.age) { ... }if (person.age != undefined) { ... }if (person.age !== undefined) { ... }if (typeof(person.age) != 'undefined') { ... }if (person.hasOwnProperty('age')) { ... }
I know all these don't do the same, e.g. if (person.age) would also fail if age does exist but it's false or null or '' or 0. And I wonder if some aren't just flat out wrong.
Note that person is known to be an existing object here, but person.age may or may not exist.