Lesson 23-In-Depth Analysis of Scope, Scope Chain, and Closures

JavaScript Scope Fundamentals

What is Scope?

Scope is a core concept in JavaScript that defines the accessibility and lifecycle of variables, functions, and objects. In simple terms, scope determines where variables are visible and accessible within the code.

JavaScript supports three main types of scope:

  1. Global Scope: Variables accessible from anywhere in the code.
  2. Function Scope: Variables defined within a function.
  3. Block Scope: Variables defined within a {} code block (introduced in ES6).
// Global scope
var globalVar = 'I am global';

function myFunction() {
  // Function scope
  var functionVar = 'I am in function';

  if (true) {
    // Block scope (ES6)
    let blockVar = 'I am in block';
    console.log(blockVar); // Accessible
  }

  // console.log(blockVar); // Error: blockVar is not defined
}

myFunction();
console.log(globalVar); // Accessible
// console.log(functionVar); // Error: functionVar is not defined

Detailed Explanation of Scope Types

Global Scope

Global scope is the outermost scope, accessible from any other scope. In a browser environment, the global scope is the window object.

// Global variable
var globalVariable = 'Global';

function checkGlobal() {
  console.log(globalVariable); // Can access global variable
}

checkGlobal();
console.log(globalVariable); // Accessible

Characteristics:

  • Accessible everywhere.
  • Overuse can lead to naming conflicts.
  • Difficult to maintain and debug.

Function Scope

Function scope refers to variables defined within a function, accessible only inside that function.

function functionScopeExample() {
  var functionVar = 'Function Scope';

  if (true) {
    var functionVar = 'Changed in block'; // var ignores block scope
    console.log(functionVar); // 'Changed in block'
  }

  console.log(functionVar); // 'Changed in block', as var ignores block scope
}

functionScopeExample();
// console.log(functionVar); // Error: functionVar is not defined

Note: Variables declared with var within a function scope are not constrained by block scope, which is a significant limitation of var.

Block Scope

ES6 introduced let and const, which provide block-level scope.

function blockScopeExample() {
  let blockVar = 'Block Scope';

  if (true) {
    let blockVar = 'Changed in block'; // Different variable
    console.log(blockVar); // 'Changed in block'
  }

  console.log(blockVar); // 'Block Scope', unaffected by inner block
}

blockScopeExample();
// console.log(blockVar); // Error: blockVar is not defined

Characteristics:

  • let and const variables are block-scoped.
  • Resolves issues with var’s hoisting and pollution.
  • Aligns with intuitive scope rules.

Scope Lookup Rules

The JavaScript engine follows these rules when looking up variables:

  1. Search the current scope first.
  2. If not found, search the parent scope.
  3. Continue up the chain until the global scope is reached.
  4. If not found in the global scope, throw an error.
var globalVar = 'Global';

function outerFunction() {
  var outerVar = 'Outer';

  function innerFunction() {
    var innerVar = 'Inner';
    console.log(innerVar); // 'Inner', current scope
    console.log(outerVar); // 'Outer', parent scope
    console.log(globalVar); // 'Global', global scope
  }

  innerFunction();
  // console.log(innerVar); // Error: innerVar is not defined
}

outerFunction();
// console.log(outerVar); // Error: outerVar is not defined

Membership Required

You must be a member to access this content.

View Membership Levels

Already a member? Log in here
Share your love