You are on page 1of 2

Scope in JavaScript

Scope in JavaScript determines the accessibility or visibility of variables,


functions, and objects at various parts of the code. It's a fundamental concept
that defines where variables and functions can be accessed within the code. There
are two main types of scope in JavaScript:

1. Global Scope: Variables or functions declared in the global scope are accessible
from anywhere in the code. This means they are available throughout your JavaScript
files.

2. Local (or Function) Scope: Variables or functions declared within a function are
accessible only within that function and not from outside it. This includes
variables declared with `var` within a function.

With the introduction of ES6 (ECMAScript 2015), two new types of scope were
introduced with the `let` and `const` keywords:

3. Block Scope: Variables declared with `let` or `const` are scoped to the block
(`{}`) in which they are defined, as well as in any contained sub-blocks. This
allows for more precise control over where variables are accessible.

Example of Global Scope:

var globalVar = 'I am in the global scope';

function checkScope() {
console.log(globalVar); // Accessible here
}

checkScope(); // Outputs: I am in the global scope

Example of Local Scope:**

function localScope() {
var localVar = 'I am in the local scope';
console.log(localVar); // Accessible here
}

localScope(); // Outputs: I am in the local scope


console.log(localVar); // Uncaught ReferenceError: localVar is not defined

Example of Block Scope:

function blockTest() {
if (true) {
let blockVar = 'I am in a block';
console.log(blockVar); // Accessible here
}
console.log(blockVar); // Uncaught ReferenceError: blockVar is not defined
}

blockTest();

The Scope Chain in JavaScript

The Scope Chain in JavaScript is a concept used to explain how child scopes have
access to variables defined in their parent scopes, forming a "chain" of scopes.
When a variable is used, the JavaScript engine looks up the scope chain to find the
variable's value, starting from the current scope and moving outwards until it
reaches the global scope. If the variable is not found, a `ReferenceError` is
thrown.

The scope chain is created based on the lexical scoping of the code, meaning the
structure of the code itself determines the scope chain at the time the code is
written.

Example of Scope Chain:

var globalVar = 'global';

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

function innerFunction() {
var innerVar = 'inner';
console.log(innerVar); // inner
console.log(outerVar); // outer
console.log(globalVar); // global
}

innerFunction();
}

outerFunction();

In this example, `innerFunction` has access to variables `innerVar` (its own


scope), `outerVar` (the outer function's scope), and `globalVar` (the global
scope), demonstrating how the scope chain works.

Understanding scope and the scope chain is crucial for managing variable visibility
and lifetimes, debugging code, and writing efficient, error-free JavaScript
applications.

You might also like