DEV Community

loading...

Understanding let and const.

Kamlesh Chavan
Software Developer
Updated on ・3 min read

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 first let if we need to. (let's forget about var)
  • Both let and const can not be redeclared. It gives an error.
  • let Variables values can be reassigned.
  • You can not reassign values to const.
  • Both let and const are block scope. Whereas var 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. 🎉

Discussion (8)

Collapse
astroparam profile image
Parmeshwar Patidar

Good explanation but what about Hoisting ? where variables declared with var are moved to the top of their scope before code execution. You should add it to give better understanding of var keyword.

Collapse
kamleshchavan profile image
Kamlesh Chavan Author

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.

Thanks.

Collapse
chinedumonyema profile image
Chinedum Onyema

Great article Mr Kamlesh

Collapse
kamleshchavan profile image
Kamlesh Chavan Author

Thank you 🎈

Collapse
tdwright profile image
Tom Wright

Great explanation Kamlesh. Love the examples too.

Collapse
kamleshchavan profile image
Kamlesh Chavan Author

Thank you ✌

Collapse
aliahmedk4 profile image
aliahmedk4

Nice article 👍.great article in making it simple n understandable

Collapse
kamleshchavan profile image
Kamlesh Chavan Author

Thank you ✌