Question 07 / 14
When can an undeclared assignment create a global?
Topic: Global bindings
Sloppy script assignment to an unresolvable name can create a property on the global object.
Solve it with the execution visualizerStep through every binding, function call, and console output below.Predict the output
Why can b be printed even though there is no var, let, or const declaration?
console.log(a)
b = 10
console.log(b)
var a = 20
console.log(a)Reveal the answer and explanation
Expected output
undefined
10
20Why this happens
The global var a is prepared before execution, so the first log prints undefined.
In sloppy script code, assigning to the unresolvable name b creates a global-object property with value 10.
The later var assignment stores 20 in a, so the final log prints 20.
What this question tests
- sloppy-mode assignment
- global-object properties
- declared versus undeclared names
Runtime assumptions
This requires non-strict classic script semantics. Strict mode and ES modules throw ReferenceError for b = 10.