JAVASCRIPT SCOPE
The scope determines the accessibility (visibility) of the variables.
JavaScript has 3 types of scopes:
- Block scope
- Function scope
- Global scope
BLOCK SCOPE
Prior to ES6 (2015), JavaScript only had Global Scope and Function Scope. ES6 introduced two major new JavaScript keywords: let and const. These two keywords provide the Block Scope in JavaScript. Variables declared inside a {} block cannot be accessed from outside the block:
{
let x = 2;
}
// x can NOT be used here
Variables declared with the var keyword CANNOT have a block scope. Variables declared inside a {} block can be accessed from outside the block with the keyword var.
{
var x = 2;
}
// x CAN be used here
LOCAL SCOPE
Variables declared inside a JavaScript function become LOCAL for the function.
// code here can NOT use carName
function myFunction() {
let carName = “Volvo”;
// code here CAN use carName
}
// code here can NOT use carName
Local variables have a function scope and are only accessible from within the function. Since such variables are recognized only within their functions, variables with the same name can be used in different functions. Local variables are created when a function is started and deleted when the function is completed.
FUNCTION SCOPE
JavaScript has a function scope: each function creates a new scope (scope of visibility). Variables defined within a function are not accessible (visible) from outside the function. Variables declared with var, let, and const are quite similar when declared inside a function. They all have a function scope:
function myFunction() {
var carName = “Volvo”; // Function Scope
}
function myFunction() {
let carName = “Volvo”; // Function Scope
}
function myFunction() {
const carName = “Volvo”; // Function Scope
}
GLOBAL JAVASCRIPT VARIABLES
A variable declared outside of a function becomes GLOBAL.
let carName = “Volvo”;
// code here can use carName
function myFunction() {
// code here can also use carName
}
A global variable has a Global Scope: All scripts and functions of a Web page can access it.
GLOBAL SCOPE
Variables declared Globally (outside of any function) have Global Scope. Global variables can be accessed from anywhere in a JavaScript program. Variables declared with var, let, and const are quite similar when declared outside a block. They all have a global scope:
var x = 2; // Global scope
let x = 2; // Global scope
const x = 2; // Global scope
In JavaScript, objects and functions are also variable. The scope determines the accessibility of variables, objects and functions from different parts of the code.
GLOBAL AUTOMATICALLY
If you assign a value to a variable that has not been declared, it will automatically become a GLOBAL variable. This code example declares a global variable carName, even if the value is assigned inside a function.
myFunction();
// code here can use carName
function myFunction() {
carName = “Volvo”;
}
STRICT MODE
All modern browsers support running JavaScript in “Strict Mode”. You will learn more about how to use strict mode in a later post of this tutorial. In “Strict Mode”, undeclared variables are not automatically global.
GLOBAL VARIABLES HTML
With JavaScript, the global scope is the JavaScript environment. In HTML, the global scope is the window object. Global variables defined with the var keyword belong to the window object. Global variables defined with the let keyword do not belong to the window object.
WARNING:
DO NOT create global variables unless essential.
LIFE CYCLE OF JAVASCRIPT VARIABLES
The lifetime of a JavaScript variable begins when it is declared. Function (local) variables are deleted upon completion of the function. In a web browser, global variables are deleted when the browser window (or tab) is closed. Function arguments (parameters) work like local variables within functions.
Leave A Comment