JavaScript Scope is the area where a variable (or function) exists and is accessible. We can layer the scope in a system which means the child scope can access the parent scope but not vice-versa.
Javascript has different scopes
Global Scope
Those variables which are declared outside the function or blocks or you can say curly braces({}) are having a global scope.
In a JavaScript program, global variables can be accessed from anywhere.
The scope of var, let, and const are quite equivalent when declared outside the function.
Example: In this example, we will declare all the variables outside the function and now we can access those variables from anywhere in the Javascript program.
JavaScript
var global_variable1 = "Geeksforgeeks";
let global_variable2 = "Geeks";
const global_variable3 = "GFG";
function check_global_variables(){
console.log(global_variable1);
console.log(global_variable2);
console.log(global_variable3);
}
check_global_variables();
OutputGeeksforgeeks
Geeks
GFG
Function Scope
Those variables that are declared inside a function have local or function scope which means variables that are declared inside the function are not accessible outside that function.
When declared inside a function, variables declared with var, let, and const look quite similar.
Note: Functions and Objects are also variables in Javascript.
Variables which are declared within the function have function scope. When var declared in the function have a function scope. Function uses the curly braces to store the function body . Therefore, block scope and function scope is same for the function body.
Example: In this example, we will declare a variable inside the function and try to access it from outside the function.
JavaScript
function check_function_scope(){
var variable1 = "Geeksforgeeks";
let variable2 = "Geeks";
const variable3 = "GFG";
console.log(variable1);
console.log(variable2);
}
check_function_scope();
console.log(variable3);
Output: In the above example, we tried to access the variable outside the function but it gives the reference error because of the function scope.
Geeksforgeeks
Geeks
ReferenceError: variable3 is not defined
Block Scope
Before the ECMA6(2015) we have only global and function scope but when the Let and Const keywords were introduced in ES6 they provide the block scope.
Variables that are declared inside the { } (curly braces) can only access inside that scope not from the outside of it.
Variables declared with var do not have block scope only let and const have block scope.
Example: In this example, we will declare the variable inside the block scope.
JavaScript
{
var variable_1 = "GFG";
const variable_2 = "Geeksforgeeks";
let x=2;
x*=2;
console.log(x);
console.log(variable_2);
}
console.log(variable_1);
Output: In the above example, we have successfully accessed the variable with the var keyword because var does not have a block scope.
4
Geeksforgeeks
GFG
Lexical Scope
The variable is declared inside the function and can only be accessed inside that block or nested block is called lexical scope.
Example: In this example, we will declare the variable inside the function and going to access inside the nested function.
JavaScript
function outerFunction() {
const outerVariable = "Hello";
function innerFunction() {
const innerVariable = "Geeks";
console.log(`${outerVariable} ${innerVariable}`);
}
innerFunction();
}
outerFunction();
Output:
Hello Geeks
Global Variables in HTML
The Global variables defined with the var keyword are part of the window object. The window object is the global scope in HTML.
Lifetime of JavaScript Variables
- A JavaScript variable’s lifetime begins when it is declared.
- When the function is completed, the function (local) variables are erased.
- When you close a browser window, global variables are erased.
Similar Reads
JavaScript | Namespace
Namespace refers to the programming paradigm of providing scope to the identifiers (names of types, functions, variables, etc) to prevent collisions between them. For instance, the same variable name might be required in a program in different contexts. Using namespaces in such a scenario will isola
1 min read
Lexical Scope in JavaScript
Lexical scope is a fundamental concept in programming that determines the accessibility of variables and functions based on where they are defined in the source code. In simple terms, lexical scope is the scope of a variable or function determined at compile time by its physical location in the code
4 min read
JavaScript Statements
JavaScript statements are programming instructions that a computer executes. A computer program is essentially a list of these "instructions" designed to perform tasks. In a programming language, such instructions are called statements. Types of Statements1. Variable Declarations (var, let, const)In
4 min read
JavaScript Super Keyword
The super keyword is used to call the parent class's constructor to access its properties and methods. Syntax:super(arguments);super.parentMethod(arguments);Arguments: This keyword can accepts all the arguments that has been used to create a constructor. Example: The code defines Person and FashionD
1 min read
JavaScript Variables
Variables in JavaScript can be declared using var, let, or const. JavaScript is dynamically typed, so variable types are determined at runtime without explicit type definitions. JavaScript var keywordJavaScript let keywordJavaScript const keyword [GFGTABS] JavaScript var a = 10 // Old style let b =
5 min read
JavaScript Var Statement
The var keyword is used to declare variables in JavaScript. It has been part of the language since its inception. When a variable is declared using var, it is function-scoped or globally-scoped, depending on where it is declared. Syntax var variable = value;It declares a variable using var, assigns
7 min read
JavaScript Nested Classes
Let us try to understand, what is class. A class in JavaScript is a type of function, which can be initialized through both function keywords as well as through class keywords. In this article, we will cover the inner class in javascript through the use of a function keyword. Here's an example of th
3 min read
JavaScript Memoization
As our systems mature and begin to do more complex calculations, the need for speed grows, and process optimization becomes a need. When we overlook this issue, we end up with applications that take a long time to run and demand a large number of system resources. In this article, we are going to lo
6 min read
Scope : Variable Masking in JavaScript
In this article, we will learn about Variable Masking which is also known as Variable Shadowing i.e. a variable with the same name will shadow the variable in the outer scope. When a variable is masked, the masked variable is completely inaccessible using that variable name. The scope is hierarchica
3 min read
What is Variable Scope in JavaScript ?
Variable scope is the context of the program in which it can be accessed. In programming, a variable is a named storage location that holds data or a value. Think of it as a container that you can use to store and manipulate information in your code. Variables allow you to work with data in a flexib
4 min read