When checking if the this.data
is undefined
, using !(this.data)
seems like a valid approach:
if (!(this.data)) { console.log("DOH!"); }
However, it's important to consider all possibilities. What if the key is actually set to false
? Just relying on !(this.data)
won't cover that case. It might be better to use the in
keyword as suggested in this answer.
if (!("data" in this)) { console.log("DOH!"); }
For example:
obj = { a: false, b: undefined }
if (!("a" in obj)) { console.log("a does not exist"); }
if (!("b" in obj)) { console.log("b does not exist"); }
if (!("c" in obj)) { console.log("c does not exist"); }
The output would be:
c does not exist