AI solution for JavaScript Quiz on BFE.dev
73. window name
The code will output:
"undefined1"
"21"
"undefined1"
"2undefined"
Explanation:
-
In the first IIFE (Immediately Invoked Function Expression), variable
ais declared usingvar, which means it is locally scoped to the function. Therefore, whenais logged before it is declared, it refers to the local variableawith no value assigned yet, henceundefined. Afterais declared and assigned the value'2', it is logged along with the globalavariable, which has a value of1. So the first log outputsundefined1and the second log outputs21. -
In the second IIFE, variable
nameis also declared usingvar, which is again locally scoped to the function. Similarly to the first IIFE,nameis logged before it is declared, so it isundefined. Afternameis declared and assigned'2', it is logged along with the globalnamevariable which has a value of1. Therefore the first log outputsundefined1. In the second log,name(local) is concatenated withthis.name(global), which isundefinedbecause there is no globalnamevariable. Therefore the second log outputs2undefined.