For example:
var myObj = {
yes: undefined
}
console.log(typeof myObj.yes === 'undefined') //--> true
console.log(typeof myObj.nop === 'undefined') //--> true too
Is there a way to detect if myObj.nop is not defined as undefined ?
You would use in:
if('yes' in myObj) {
// then we know the key exists, even if its value is undefined
}
Keep in mind, this also checks for properties in the object's prototype, which is probably fine in your case, but in case you only want to check properties that are directly set on that specific object, you can use Object.prototype.hasOwnProperty:
if(myObj.hasOwnProperty('yes')) {
// then it exists on that object
}
Here's a good article on the difference between the two.
hasOwnPropertyinstead.