Created: 2023-01-27 09:49
Status: #concept
Subject: Programming
Tags: C JavaScript Global Scope Function Scope Block Scope File Scope Lexical Scope

Scope

Definition

The section of the code where Values, Variables & Expressions are "visible" or can be referenced.

JavaScript

The var keyword makes a Variable that is always in the Global Scope.

  • We should use let or const to enclose variables in a specific Scope.

/* var is always in the Global Scope */
{
  var x = 1;
}
console.log(x); // 1

/* compared to let or const */
{
  const x = 1;
}
console.log(x); // ReferenceError: x is not defined
// Scope A
var myFunction = function () {
  // Scope B
  var myOtherFunction = function () {
    // Scope C
  };
};

References