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
- We should use
let
orconst
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
};
};