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 = "w3wiki" ; 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
w3wiki
GFG
Javascript Scope
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.
Contact Us