Difference between var, let and const in Javascript.

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.

var :

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 varis 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 letis going to overcome this drawback of var.

let :

Today most of the developers prefer letfor declaration because it overcomes the drawback of var.

Now, let's see how letworks:

The scope of the letis 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 ifblock 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 booleanValueis 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 letovercame it.

You can see here, we declared the sentencevariable again but it did not give us any error because the first sentencevariable's scope has been ended within the ifblock. So that's why it has not given any error. And after declaring we can also update our variable with another value.

const :

By the name only you can get an idea of it, const refers to the constant value. So whenever you initialize your variable with constthat variable's value is now constant it cannot be changed.

The scope of the constis same as letwhich is scope based.

So as I said value of constcannot be changed:

As you can see if you are trying to reassign the constvariable it is giving you an error.

What if we try to re-declare it :

So re-declaring constvariable is also not possible.

But constworks different for Object and Arrays.

If we want to modify an object which is declared using consthow 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 personvariable 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 constbut we cannot initialize it again.

One more thing about const:

You cannot initialize constvariable after declaring it, you have to initialize the constvariable with some value at the start or else it will give error.

Thank you for reading this blog:

- Mayuresh Kunder