askvity

What are JavaScript scopes?

Published in JavaScript Scope 5 mins read

JavaScript scope defines the accessibility of variables and functions within your code. It determines where in your program a variable or function is visible and can be used.

Based on the provided reference, Scope in JavaScript refers to the current context of code, which determines the accessibility of variables to JavaScript. This means scope dictates which parts of your code can "see" and interact with specific variables.

Historically, JavaScript primarily had two types of scope: Global and Function Scope. With the introduction of let and const in ECMAScript 2015 (ES6), Block Scope became a significant addition, refining the concept of 'local' scope.

Understanding the Types of JavaScript Scope

Let's break down the different types of scope in JavaScript:

1. Global Scope

Variables and functions declared in the global scope are accessible from anywhere in your JavaScript code.

  • Definition: Global variables are those declared outside of any block.
  • Accessibility: Accessible throughout the entire script or multiple scripts loaded on the same page.
  • Declaration: Typically declared at the top level of a script outside of any function or block.
  • Caution: Overuse of global variables can lead to naming conflicts and make code harder to manage and debug.

Example:

let globalVariable = "I am global!";

function showGlobal() {
  console.log(globalVariable); // Accessible here
}

showGlobal();
console.log(globalVariable); // Accessible here as well

2. Local Scope (Function Scope)

Variables declared inside a function become local to that function. They are only accessible from within the function itself.

  • Definition: Local variables are those declared inside of a block. Traditionally, this block was often a function body when using the var keyword.
  • Accessibility: Accessible only within the function where they are declared.
  • Declaration: Declared inside a function using var.
  • Key Characteristic: Variables declared with var inside a function have function scope, regardless of nested blocks within the function.

Example:

function showLocal() {
  var localVariable = "I am local to the function!";
  console.log(localVariable); // Accessible here
}

showLocal();
// console.log(localVariable); // Error: localVariable is not defined outside the function

3. Block Scope

Introduced with let and const in ES6, block scope means that variables declared inside curly braces {} (like if statements, for loops, or simple code blocks) are only accessible within that block.

  • Definition: Variables declared inside a block using let or const have block scope.
  • Accessibility: Accessible only within the specific block {} where they are defined.
  • Declaration: Declared inside a block using let or const.

Example:

if (true) {
  let blockVariable = "I am block-scoped!";
  const anotherBlockVariable = "Me too!";
  console.log(blockVariable); // Accessible here
  console.log(anotherBlockVariable); // Accessible here
}

// console.log(blockVariable); // Error: blockVariable is not defined outside the block
// console.log(anotherBlockVariable); // Error: anotherBlockVariable is not defined outside the block

Scope Chain

JavaScript uses a concept called the scope chain to determine variable accessibility. When you try to access a variable, the JavaScript engine starts looking for it in the current scope. If it doesn't find it, it moves up to the immediate outer scope, and continues this process until it reaches the global scope. If the variable is not found in the global scope, a ReferenceError is thrown.

Think of it like layers:

  1. Innermost (current) scope
  2. Outer scope
  3. ...
  4. Global scope

Inner scopes can always access variables from outer scopes, but outer scopes cannot access variables from inner scopes.

Example illustrating Scope Chain:

let outerVariable = "I am outer";

function outerFunction() {
  let middleVariable = "I am middle";

  function innerFunction() {
    let innerVariable = "I am inner";
    console.log(innerVariable); // Accessible
    console.log(middleVariable); // Accessible (from outer scope)
    console.log(outerVariable); // Accessible (from global scope)
  }

  innerFunction();
  console.log(outerVariable); // Accessible (from global scope)
  // console.log(innerVariable); // Error: innerVariable is not defined
}

outerFunction();
console.log(outerVariable); // Accessible (from global scope)
// console.log(middleVariable); // Error: middleVariable is not defined

Comparing Scope Types

Here's a quick summary of the main scope types:

Scope Type Keyword(s) Accessibility Defined By
Global Scope var, let, const Anywhere in the code Outside any function/block
Function Scope var Inside the function it's declared in Function block {}
Block Scope let, const Inside the block {} it's declared in Any block {}

Understanding scope is crucial for writing predictable and maintainable JavaScript code, helping you avoid unintended variable interactions and errors.

Related Articles