- Global Scope: Variables declared Globally (outside of any function) have Global Scope and Global variables can be accessed from anywhere in a program. Similar to function scope variables declared with var, let and const are quite similar when declared outside a block.Scope outside the outermost function attached to the window.
- Local Scope: Variables declared inside a function become local to the function. Local variables are created when a function starts and deleted when the function is executed. Local variables have Function Scope which means that they can only be accessed from within the function. Inside the function being executed.
Example 1: We have a global variable defined in the first line in the global scope. Then we have a local variable defined inside the function fun().
Output: When we execute the function fun(), the output shows that both global, as well as local variables, are accessible inside the function as we are able to console.log them. This shows that inside the function we have access to both global variables (declared outside the function) and local variables (declared inside the function). Example: Let’s move the console.log statements outside the function and put them just after calling the function.
Output: We are still able to see the value of the global variable, but for local variable console.log throws an error. This is because now the console.log statements are present in global scope where they have access to global variables but cannot access the local variables. Word of caution: Whenever you are declaring variables, always use the prefix let. If you don’t use the let keyword, then the variables are by default created in the global scope. For instance, in the above example.
Example: let’s just remove the keyword let before the declaration of localLet.
Output: In this example, we have declared a local as well as global variable “globalLet”. What matters here is the scope in which we are accessing it. In this example, we are accessing it in global scope, so it will output the global variable as a local variable is not present in its scope. Example: Let’s move the console.log statement inside the function fun().
Output: In this example, as we didn’t use the keyword let, the variable “i” was assumed to be declared in the global scope, and thus the output was 100. Example:
Output: In this example, “i” became a local variable and thus was not accessible outside the scope of that functionIn the first example, as we didn’t use the keyword let, the variable “i” was assumed to be declared in the global scope, and thus the output was 100. In the second example, “i” became a local variable and thus was not accessible outside the scope of that function.
Output: After ES2015, we started using let instead of var for declaring variables, and also now the if block is also counted as a block scope, hence in the above example we get an error instead of the value 100. In earlier versions If we change the let to var we will get 100 as output as “if..else” block was not considered a block scope earlier, only functions were considered block scope.
Also, the const keyword has the same scope as the let keyword and is also a block scope.
Please Login to comment...