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
a
is declared usingvar
, which means it is locally scoped to the function. Therefore, whena
is logged before it is declared, it refers to the local variablea
with no value assigned yet, henceundefined
. Aftera
is declared and assigned the value'2'
, it is logged along with the globala
variable, which has a value of1
. So the first log outputsundefined1
and the second log outputs21
. -
In the second IIFE, variable
name
is also declared usingvar
, which is again locally scoped to the function. Similarly to the first IIFE,name
is logged before it is declared, so it isundefined
. Aftername
is declared and assigned'2'
, it is logged along with the globalname
variable which has a value of1
. Therefore the first log outputsundefined1
. In the second log,name
(local) is concatenated withthis.name
(global), which isundefined
because there is no globalname
variable. Therefore the second log outputs2undefined
.