Based on this answer I know I can check if a property exists using the in operator. So I see in a lot of code that it is common to use, for example:
if ("gapi" in window) {
gapi.login(...);
}
But wouldn't be better to do it this way?
if (window.gapi) {
gapi.login(...);
}
The only difference that I am aware of is that the second one will return false if the variable exists but its value is undefined
, false
, 0
, null
or other value that evaluates to false; and the first one will return true, right?
So, is there any other difference? Which one should I use?