ES6 introduced two new ways of declaring variables in JavaScript let and const.
Is there any issue with var? No, 100 thousands line of code is working in production javascript without any problem using var. But its possible to accidentally misuse or create unnecessary weird situations.
Let's kick things by understanding issues with var
.
- Declaration and value assignments :
In the above code, we get Kamlesh
printed first and then Something else
get printed in console as we have reassigned the same variable var name
with a different value. We can go ahead and reassign this var name
to function, number, object, or anything.
We just not only can reassign var based variables but also redeclare them and make new var name = 'something'
. This is perfectly valid with var
based variables, and this can get us in the wired situation like redeclaring the same variables by not knowing we have already used the same variable name.
- Scope:
In the above code, we can see that we have used var firstName
variables both inside and outside the if
block, and console.log(firstName)
are printing results for both without any error.
var
based variables are function scope, meaning they are not available outside the function. Also, they are not block scope variables, meaning they are available outside the block.
Things with let
- Declaration and value assignments :
In the above code, when we assign value to, name = 'Something'
and we get the desired output in the console. But when we try to redeclare, let
we get an error "already declared".
We can reassign let
but can not redeclare let
based variables. If we try to redeclare let
based variables, we get an error "duplicate declaration".
- Scope
Here in the above code, you can see when we declare var firstName
inside the block and using it inside block prints the result, but using it outside block is giving an error "variable not defined".
This means that let
variables are block scope variables meaning they are only available inside their declaration scope block.
If we need to use firstName outside the block, we need to declare it up above the if block, and it should work in case of the above example.
Things with const
- Declaration and value assignments :
Here we can see that we can not reassign const
variables, nor can we redeclare them. If we try to do so, we should expect an error.
Point to note here is the const
based variables can not be redefined and also can not be reassigned.
- Scope
The const
based variables are also block scope variables, and they are only available with the block they are defined. If we try to access then outside the block, an error is expected.
It's always a good practice to start defining the variable as const and eventually if we determine that some of those values need to be reassigned then we use let
.
Many people get confused with const value reassignment. Please see the below code.
You can assign an object to const and you can change the value of properties inside the object but can not reassign the value to const person
. There is another example of const someString = 'Dummy string'
, when you try to reassign this someString
variable it gives an error.
Let's recap what we learned here,
- Start declaring variables as
const
unless their values need to be reassigned. - So
const
firstlet
if we need to. (let's forget aboutvar
) - Both
let
andconst
can not be redeclared. It gives an error. -
let
Variables values can be reassigned. - You can not reassign values to
const
. - Both
let
andconst
are block scope. Whereasvar
is function scope.
Please read through hoisting in JavaScript. Hoisting is JavaScript's default behaviour of moving declarations to the top (not literally). It is another important concept to know about the variable declaration.
Thank you for reading; this is my first blog ever. 🎉
Oldest comments (8)
Great explanation Kamlesh. Love the examples too.
Thank you ✌
Nice article 👍.great article in making it simple n understandable
Thank you ✌
Good explanation but what about
Hoisting
? where variables declared withvar
are moved to the top of their scope before code execution. You should add it to give better understanding ofvar
keyword.Hi Parmeshwar,
Thanks for reading.
Hoisting
concept I had planned to add it in another blog post. You can read in the below article. I will add the link of same it this post.Hoisting in JavaScript
Kamlesh Chavan ・ May 3 ・ 2 min read
Thanks.
Great article Mr Kamlesh
Thank you 🎈