JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsjavascript
PrevNext

Learn the concept

Scope & Scope Chain

javascript
mid
scope

What is the difference between block scope and function scope?

scope
var
let
const
block-scope
function-scope
hoisting
temporal-dead-zone
Quick Answer

Function scope (var) means a variable is accessible throughout the entire function it's declared in. Block scope (let/const) limits a variable to the nearest enclosing block (if, for, while, or bare {}). Block scoping prevents common bugs like loop variable leaking and accidental overwrites.

Detailed Explanation

Function Scope (var):

  • Scoped to the nearest enclosing function (or global if no function)
  • Hoisted to the top of the function and initialized as undefined
  • Accessible before its declaration line (returns undefined)
  • Ignores block boundaries — leaks out of if/for/while blocks
  • Can be re-declared in the same scope without error

Block Scope (let / const):

  • Scoped to the nearest enclosing block {}
  • Hoisted but NOT initialized — accessing before declaration throws ReferenceError
  • The period between entering the block and the declaration is called the Temporal Dead Zone (TDZ)
  • Cannot be re-declared in the same scope
  • const additionally prevents reassignment (but does NOT make objects immutable)

The Classic Loop Problem:

  • Using var in a for-loop creates a single variable shared across all iterations
  • Closures over that variable all see the final value
  • let creates a fresh binding per iteration, fixing this naturally

Scope Chain:

  • When a variable is referenced, JavaScript looks up the scope chain
  • Inner scopes can access outer scope variables (closure)
  • Outer scopes cannot access inner scope variables
  • Block scope creates additional scope boundaries within functions

Best Practice:

  • Default to const for values that won't be reassigned
  • Use let when reassignment is needed
  • Avoid var in modern code — block scoping prevents an entire class of bugs

Code Examples

Function scope vs block scopeJavaScript
function example() {
  // var is function-scoped — accessible throughout the function
  if (true) {
    var x = 10;
    let y = 20;
    const z = 30;
  }

  console.log(x); // 10 — var leaks out of the if-block
  // console.log(y); // ReferenceError — let is block-scoped
  // console.log(z); // ReferenceError — const is block-scoped

  // var hoisting — accessible before declaration
  console.log(a); // undefined (hoisted, initialized as undefined)
  var a = 5;

  // let/const TDZ — NOT accessible before declaration
  // console.log(b); // ReferenceError: Cannot access 'b' before initialization
  let b = 5;
}

Real-World Applications

Use Cases

Preventing Variable Leaking

Block scoping with let/const prevents loop counters, temporary variables, and intermediate values from polluting the enclosing function scope

Event Listener Closures

Using let in loops that attach event handlers ensures each handler captures its own iteration value

Configuration Objects

Using const for configuration prevents accidental reassignment while still allowing property updates via mutation

Mini Projects

Scope Visualizer

intermediate

Build a tool that takes JavaScript code and visually highlights different scope levels and variable accessibility

Closure Quiz Generator

beginner

Create an interactive quiz that presents var/let scoping puzzles and explains the output

Industry Examples

ESLint

The no-var rule enforces block-scoped declarations, preventing function-scope bugs in modern codebases

TypeScript

TypeScript's strict mode builds on block scoping to provide even stronger guarantees about variable usage and initialization

Resources

MDN - var

docs

MDN - let

docs

MDN - const

docs

Related Questions

How does JavaScript hoisting work?

junior
hoisting

What is a closure in JavaScript and why are they useful?

mid
closures
Previous
What are call, apply, and bind and how do they differ?
Next
What are WeakMap and WeakSet, and how do they differ from Map and Set?
PrevNext