Hi there. It is easier to share knowledge than to learn anything from resources. I will therefore be sharing my knowledge on scope today, a crucial JavaScript topic.
Now let's proceed.
Scope dictates the level of visibility and accessibility. Because programming languages specify which parts of a variable can be accessed, that is how it works..
Basically 3 scope we have but sometimes learners faces an contradictory situation in between them.
Block Scope
Function Scope
Global Scope
We can infer from the term "Block Scope" that access is restricted to a specific portion.Following the introduction of ES6, function scope and global scope are added to the block scope.
In ES6, the terms "let" and "const" are introduced to help us grasp the block scope.
Let's have a look.
{
let language = "JavaScript";
}
{
var language = "JavaScript";
}
Do you notice a difference? Indeed, there are differences in the variable types; one uses let, while the other uses var.
Since no one has the authority to call the let outside of the {} block, let is our block scope here rather than the var type.
And var can be access able from the outside of the block so it doesn't create the block scope.
Now let's discuss local scope.
The variable that is granted permission just within the function is referred to as local scope, meaning that it can only be accessed from within its immediate surroundings.
// Not allow to use the code
function myFunction(){
let carName: "Toyota";
// allow to use the code//
}
// Not allow to use the code
Local scope life time is very limited. With the end of function lifetime the local scope lifetime is also end.
Time to learn about function scope. From the name it's quite simple to recognize that scope.
Those variable have the permission to call them from inside the function only rather then outside of the function.
In that case var, let and const are doing same thing. When they declared in the function the function scope is created and after end of the performance of the function the scope is finished.
For understand the global scope it's necessary to have knowledge about global variables.
The variable that can decalear outside of the function is always a global variable. It's accessible from anywhere of your code.
So where global variable is declare global scope is start from there and in that case the let,const and var are same.
var fruit = "Apple"; // Global scope
const fruit = "Apple"; // Global scope
let fruit = "Apple"; // Global scope
There another thing is that "Automatically Global"
If you assign value in a variable without declare the variable that's consider automatically global variable rather if it stay inside the function.
programming();
// can accessible from anywhere
function programming(){
language = "JavaScript";
}
The language is inside the function but it can accessible from anywhere of the script because it's not declarer using vat let const. so it declared automatically global.
However, That's the end of today's discussion. Hopefully You will enjoy it.
Top comments (2)
Hey, nice article. You can add syntax highlights as well.
read here
github.com/adam-p/markdown-here/wi...
Thanks for your Suggestion.