This tests if the window
object has a property (filled or not) whose key is "aa"
.
This operator is very useful because it works even if the value is undefined
:
window.aa = undefined; // or just aa=undefined if you're in the global scope
console.log('aa' in window); // logs true
It also works if the property isn't enumerable :
console.log('length' in []); // logs true
In your case, there may not be an aa
value, but if the alert shows you true, the property was added to window
.
MDN reference on in
Note that the for...in
statement is different in that it doesn't really use the in
operator but is a specific construct.
MDN reference on for...in
EDIT : an explanation of your edited question (very different from the first one) :
Your confusion seems to arise from the fact you declared the var aa = 1;
in a block. You should know that the scope of a variable in JavaScript is either a function of the global scope and that declarations are hoisted. So your code is in fact equivalent to
var aa = undefined;
if (!("aa" in window)) { // aa is in window, so we don't enter here
alert('oh my god');
aa = 1;
}
alert("aa" in window); // yes, the property exists, it's true
alert(aa); // aa is still undefined