QUESTION 04 / 20 · SCOPE · INTERMEDIATE

Beyond the block

What will this code print? Read each line, then make your prediction.

Where this matters

Old code may use var inside a condition. Do not assume that braces give var the same isolation as let.

JavaScript · read-only example
var score = 1;
if (true) { var score = 3; }
console.log(score);
Your prediction

Read the full explanation

Var is not block-scoped. Both declarations refer to the same binding in this script.

Trace the code

  1. The script begins with a score binding containing 1.
  2. The condition is true, so its body executes. The second var declaration refers to the same binding and assigns 3.
  3. Leaving the if block does not restore the earlier value.

Why the other predictions differ

  • 1 — That reasoning fits a separate inner let, but not var.
  • SyntaxError — Repeated var declarations in this scope are allowed.
Expected console output
3

Reading this explanation does not record mastery. A correct submitted attempt can do so, including after review.

Change one thing

Change both declarations to let and observe what survives outside the block.

Predict the result, then run this self-contained snippet in your browser’s developer console. It uses only sample values and makes no network requests.

let score = 1; if (true) { let score = 3; } console.log(score);
Check the variation’s output
1

Explain which binding, value, or operation changed before moving to the next challenge.