So, var is there in javascript since the beginning, let and const were introduced in ES2015/ES6. Let us see what is the difference between these variable declarations.
Scope of the var
is function based.
Basically meaning of scope is nothing but the place where your variable is accessible. So your variable is only accessible inside the scope, it is not accessible outside of it.
Now, we know that var
has a scope which is function based which means any variable declared inside a function is accessible only inside the function.
Let's understand this with an example:
What we can see in the above example, variable is declared inside the function so therefore it was accessible and it gave the output.
In the above example we can see that it gave an error as we are trying to use myName
variable outside the function.
Another thing about var
is that it can be declared and updated again and again and we won't get any error.
So, this is the main problem of var
. Even if we are using the same variable unknowingly then also it will not give any error, so in the end we will not get expected output from that variable.
Let us see how let
is going to overcome this drawback of var
.
Today most of the developers prefer let
for declaration because it overcomes the drawback of var
.
Now, let's see how let
works:
The scope of the let
is block based. Here block is nothing but curly braces {}
. So scope starts from starting curly brace {
and ends within ending curly brace }
.
You can see when we try to console.log(sentence)
inside the if
block that time we get proper output but the moment we try to do it outside the block it gave an error.
Now let's see how drawback of var
is overcomed:
Here in the above image we can see that variable booleanValue
is initialized globally, so now we are trying to declare it again but we are getting an error that it has been already declared. So this is how let
overcame it.
You can see here, we declared the sentence
variable again but it did not give us any error because the first sentence
variable's scope has been ended within the if
block. So that's why it has not given any error. And after declaring we can also update our variable with another value.
By the name only you can get an idea of it, const
refers to the constant value. So whenever you initialize your variable with const
that variable's value is now constant it cannot be changed.
The scope of the const
is same as let
which is scope based.
So as I said value of const
cannot be changed:
As you can see if you are trying to reassign the const
variable it is giving you an error.
What if we try to re-declare it :
So re-declaring const
variable is also not possible.
But const
works different for Object and Arrays.
If we want to modify an object which is declared using const
how can we do it let's see:
Well this is not the right way to modify it as we are initializing the whole another object again to the person
variable so that's why it is giving an error. The proper way to modify it is given in below image:
So you can see that our person object was successfully modified.
Now, let's take an array as an example:
It is same for the array, we cannot reinitialize the array. The proper way to modify it is given in below image:
Yes, now you can see that array has been modified.
So what do we come to know here is we can modify our Arrays and Objects which we declared using const
but we cannot initialize it again.
One more thing about const
:
You cannot initialize const
variable after declaring it, you have to initialize the const
variable with some value at the start or else it will give error.
Thank you for reading this blog: