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
20

Why 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.

Specification references

JavaScript Code EditorDirectly edit or paste custom code
Step 0 of 0
StepSpaceRun
Press → (Step) or Space (Run)
Phase 1: Compilation(Hoisting & Memory Setup)
Phase 2: Execution(Line-by-line Evaluation)
Execution Step Breakdown

Press "→" / click "▶" to step line-by-line, or press "Space" / click "▶ Run" for auto-play.

Scope Chain
Lexical Environment Chain: Inner Scope → Outer [[Scope]] → Global (GO)
No active scope chain
CALL STACKStack of Execution Contexts (LIFO)
LIFO Stack Frames
Global Execution Context (GEC)
▶ Active Frame
HEAP & MEMORY SPACEAllocated Objects (GO, AO, FunctionObjects, ER)
ECMAScript Heap Area
No objects in memory
Console Output
console.log(...) Stream
No output generated yet