×

Scope in JavaScript

Introduction

Before diving deeper, let’s clearly define what scope means in practice:

Scope answers one simple question: “From where can I access this variable?”

If you understand where a variable is accessible and where it is not, you understand scope.


The Three Core Scopes in JavaScript

In this section, we will discuss about Global, Function, Block and Lexical scope.

1. Global Scope

Definition: Global scope is the outermost scope. Any variable declared outside a function or block belongs to the global scope and is accessible anywhere in the file.

Key points:


var in Global Scope

var gVar = 10; // Re-declare var gVar = 20; // allowed // Reassign gVar = 30; // allowed console.log(gVar); // 30

Behavior:


let in Global Scope

let gLet = 10; // Reassign gLet = 20; // allowed // Re-declare // let gLet = 30; // not allowed console.log(gLet); // 20

Behavior:


const in Global Scope

const gConst = 10; // Reassign // gConst = 20; // not allowed // Re-declare // const gConst = 30; // not allowed console.log(gConst); // 10

Behavior:


2. Function Scope

Definition: Function scope means variables declared inside a function are accessible only within that function.

Key points:


var in Function Scope

function exampleVar() { var a = 10; // Re-declare var a = 20; // allowed // Reassign a = 30; // allowed console.log(a); // 30 } exampleVar(); // console.log(a); // not accessible

Behavior:


let in Function Scope

function exampleLet() { let b = 10; // Reassign b = 20; // allowed // Re-declare // let b = 30; // not allowed console.log(b); // 20 } // console.log(b); // not accessible

Behavior:


const in Function Scope

function exampleConst() { const c = 10; // Reassign // c = 20; // not allowed // Re-declare // const c = 30; // not allowed console.log(c); // 10 } // console.log(c); // not accessible

Behavior:


3. Block Scope

Definition: Block scope refers to variables declared inside {} such as if, for, or while blocks.

Key points:


var in Block Scope

if (true) { var x = 10; } console.log(x); // accessible

Behavior:


let in Block Scope

if (true) { let y = 10; console.log(y); // accessible } // console.log(y); // not accessible

Behavior:


const in Block Scope

if (true) { const z = 10; console.log(z); // accessible } // console.log(z); // not accessible

Behavior:


Combined Example (Global + Function + Block Together)

// Global scope var gVar = "global var"; let gLet = "global let"; const gConst = "global const"; function scopeTest() { // Function scope var fVar = "function var"; let fLet = "function let"; const fConst = "function const"; if (true) { // Block scope var bVar = "block var"; // function-scoped let bLet = "block let"; // block-scoped const bConst = "block const"; // block-scoped console.log(gVar); // accessible console.log(gLet); // accessible console.log(gConst); // accessible console.log(fVar); // accessible console.log(fLet); // accessible console.log(fConst); // accessible console.log(bVar); // accessible console.log(bLet); // accessible console.log(bConst); // accessible } console.log(bVar); // accessible (var ignores block) // console.log(bLet); // not accessible // console.log(bConst); // not accessible } scopeTest(); // console.log(fVar); // not accessible // console.log(fLet); // not accessible // console.log(fConst); // not accessible

This single example demonstrates:


Lexical Scope (How Access Is Decided)

Definition: Lexical scope means that variable access is determined by where the code is written, not where it is executed.

In JavaScript, functions remember the scope in which they were defined.

const globalValue = "global"; function outer() { const outerValue = "outer"; function inner() { console.log(globalValue); // accessible console.log(outerValue); // accessible } inner(); } outer();

Key points:


Scope Chain

Definition: When a variable is accessed, JavaScript looks for it through a chain of scopes called the scope chain.

Search order:

  1. Current scope
  2. Parent scope
  3. Global scope
  4. Error if not found
const a = "global"; function first() { const b = "function"; function second() { const c = "block"; console.log(a); // found in global scope console.log(b); // found in function scope console.log(c); // found in local scope } second(); } first();

Key points:


Shadowing (Same Name, Different Scope)

Definition: Shadowing occurs when a variable declared in an inner scope has the same name as a variable in an outer scope.

let value = "global"; function test() { let value = "function"; // shadows global value console.log(value); // function } test(); console.log(value); // global

Key points:


Summary

JavaScript Variable Summary: Scope, Reassignment, Redeclaration

Featurevarletconst
Scope typeFunction scopeBlock scopeBlock scope
Accessible outside block {}YesNoNo
Accessible outside functionNoNoNo
Global scope behaviorBecomes global object propertyDoes NOT attach to global objectDoes NOT attach to global object
HoistingYes (initialized as undefined)Yes (Temporal Dead Zone)Yes (Temporal Dead Zone)
Reassignment allowedYesYesNo
Redeclaration in same scopeYesNoNo
Block-level isolationNoYesYes
Safe for loopsNo (leaks variable)YesYes
Default recommendationAvoidUse when mutableUse by default

Reassignment vs Redeclaration (Quick Reference)

Operationvarletconst
Reassign valueAllowedAllowedNot allowed
Redeclare in same scopeAllowedNot allowedNot allowed
Modify object/array contentsAllowedAllowedAllowed
Rebind referenceAllowedAllowedNot allowed