JS Built-ins
Variablesbeginner

Variables β€” var: function-scoped (not block-scoped)

var is function-scoped

Unlike let/const, a var declaration is scoped to the nearest function, not to the nearest block. It leaks out of if/for/while blocks.

Challenge: Write varFunctionScope() that:

  1. Declares var leaked = 'yes' inside an if (true) { } block
  2. Returns leaked from outside the block (it should still be accessible)
πŸ’‘ Hints (1)
  • β€’ var declarations are hoisted to the containing function, not the block