The Difference between var, let and const.

by Saurabh Bhan

There are a lot of different and new things in Javascript and as a newbie, they are quite confusing. In this article, I will talk about the differences between Var, Let, and Const.

Before let and const, var was used for declarations, Var variable can be re-declared and updated.

After ES6 let is now preferred for variable declarations as let is block scoped meaning let lives in {} curly braces. so, the declared let is only available in that block. let declarations are hoisted at the top. Where Var is initialized as undefined, the let keyword is not initialized, and if you try to access the let variable before declaring it will throw you a Reference Error. Let can be updated but can not be redeclared.

Const means Constant values, variable declared with const means these are constant values we do not want to update. Const is also block-scoped like Let and any declaration of const is only present within the declared block. Just like let Const are hoisted at the top but are not initialized. Const cannot be reupdated or re-declared.

in summary, var is global scoped while let and const are block scoped.

I hope now you have a basic understanding of how these 3 are different.