Skip to main content

Command Palette

Search for a command to run...

Scope in JS

Published
3 min read

Scope determines:

  • Where a variable is accessible

  • Where it is visible

  • Where it can be modified

In JavaScript, scope is resolved lexically (at write time, not runtime).

Types of Scope in JavaScript

JavaScript has three main scopes:

Scope TypeCreated ByVariables
Global ScopeOutside all functions/blocksvar, let, const
Function ScopeInside a functionvar, let, const
Block ScopeInside {}let, const

Global Scope

Definition

A variable declared outside any function or block belongs to the global scope.

Example

let globalVar = "I am global";

function test() {
  console.log(globalVar);
}

test(); // I am global

Key Points

  • Accessible anywhere in the file

  • In browsers:

    • var attaches to window

    • let and const do NOT attach to window

var a = 10;
let b = 20;

console.log(window.a); // 10
console.log(window.b); // undefined

Interview Insight

Interviewers often ask:

“Why is excessive global scope dangerous?”

Answer:

  • Risk of name collisions

  • Harder to debug

  • Memory leaks

  • Breaks modularity

Function Scope

Definition

Variables declared inside a function are only accessible within that function.

Important Rule

  • var is function-scoped

  • let and const are also scoped to the function (if declared there)

Example

function example() {
  var x = 10;
  let y = 20;
  const z = 30;

  console.log(x, y, z);
}

example();
console.log(x); // ReferenceError

var Function Scope Behavior

function test() {
  if (true) {
    var a = 5;
  }
  console.log(a);
}

test(); // 5

Why?

  • var ignores block boundaries

  • It belongs to the entire function

Block Scope

Definition

Variables declared using let and const inside {} are block-scoped.

Blocks include:

  • if

  • for

  • while

  • {} standalone blocks

Example

if (true) {
  let a = 10;
  const b = 20;
}

console.log(a); // ReferenceError

Scope Chaining in JavaScript

1. What Is Scope Chaining? (Core Concept)

Scope chaining is the mechanism JavaScript uses to resolve variable lookups.

When JavaScript encounters a variable:

  1. It first looks in the current scope

  2. If not found, it looks in the outer (parent) scope

  3. This continues upward until the global scope

  4. If still not found → ReferenceError

This lookup path is called the scope chain.

Important: Scope chaining is determined at code writing time, not execution time.

2. Why Scope Chaining Exists

JS needs scope chaining to:

  • Support nested functions

  • Enable closures

  • Allow controlled access to outer variables

  • Maintain predictable variable resolution

Without scope chaining, JavaScript would not be able to:

  • Share state between functions

  • Implement encapsulation

  • Build modular, maintainable code

3. Lexical Scope (Foundation of Scope Chaining)

JavaScript is lexically scoped.

This means:

  • The scope chain is decided by where the function is written

  • Not by where it is called

4. How Scope Chaining Works Internally

For this code:

let a = 1;

function one() {
  let b = 2;

  function two() {
    let c = 3;
    console.log(a, b, c);
  }

  two();
}

one();

Lookup Order for console.log(a, b, c):

  • c → found in two

  • b → not in two, found in one

  • a → not in two or one, found in global

5. Visual Representation (Interview Explanation)

Global Scope
 └── one() Scope
     └── two() Scope

JavaScript searches bottom to top, never the reverse