Scope

Scope is a policy that manages the accessibility of variables.

Global Scope

Variables defined outside of a function are in the global scope. Variables inside the Global scope can be accessed and altered in any other scope.

let globalVar = "Global Hello";

function sayHello() {
    alert(globalVar);
    
    globalVar = "I'd rather say Hi";
    alert(globalVar);
}

Web Browser Global Scope

In the context of the JavaScript runtime within the web browser, any variables declared in your script code that is not contained within a function is in the global scope.

The window and document objects, supplied by the JavaScript runtime are also in the global scope.

Global Variables Can Collide

If there are two global variables with the same name, there is a name collision. In this case the JavaScript engine will process the scripts in order and the most recent declaration and modifications will be used.

Global variables should be avoided if possible.

Function Scope

Variables declared with the let or const keyword inside a function are only visible within the function only.

Functions can access global variables

But a variable with the same name within the function will be used in place of the global variable.

Code Block Scope

Variables declared with the let or const keyword inside a code are only visible within the code block only. The scope of a code block is equivalent to the scope of a function.

Lexical Scope

Lexical Scope has to do with scope in situations where there are nested code blocks and the accessibility of a variable outside of a code block to the code within a nested code block.

The engine determines the nesting of scopes just by looking at the JavaScript source code, without executing it.

The inner function scope can access variables from the outer function scope.

Scope Summary

  • Scopes are created by code blocks.

  • A let or const variable is scoped within a code block, function, or module.

  • A let or const variable defined within a code block is only visible within the code block.

  • Lexical Scope can be determined by looking at the code. If a variable is within the current code block, or your code is referencing a variable that is visible to your nested code block, it is within your scope.

Last updated

Was this helpful?