JavaScript Variables— var vs let
ES6 brings a lot of shiny new features, and also brings one of the confusing and interesting topics of JS — the way of declaring variables.
Variables can be considered as named storage for data. Variables in JavaScript are loosely typed, which means the variable can hold values with any data type.
To declare a variable in JS, there are two options:
- Using let keyword
- Using var keyword
In this article, we will see in detail the difference between both the keywords by citing their implementation details.
1. Scope of Defined Variable
var => variable declared with the var keyword is having global scope. Means variable decalred with a var keyword is defined throughout the program.

let => variable declared with let keyword is having block scope, means it will be limited to the block in which it is declared.

2. Introduction in JavaScript
var => var keyword was introduced with Java Script
let => let keyword was introduced in ES6 (ES 2015) version of JavaScript
3. Re-Declaration
var => Variable declared with var keyword can be re-declared and updated in the same scope.

let => Variable declared with let keyword can be updated but not re-declared.

4. Hoisted
Hoisted means using the variable even before initializing it.
var => var keywords support hoisting. The interpreter gives an undefined error in this case

let => let keyword does not support hoisting. The Interpreter gives Reference Error in this case

5. Window Object
var => variable declared globally with var keyword are added to window object

let => variable declared with let keyword are not added into window object

6. Usage
var => variables are generally declared with var keyword when the value of variable needs to be less changed and used to access globally.
let => variables are generally declared with let keyword when there is limited use of those variables such as in for loops, while loops or inside the scope of if conditions etc
Some Variables cases examples:
- If we are declaring a variable globally by using the var keyword, then changing the value of that variable at any place will reflect everywhere

2. If we are re-declaring the same variable within a function, then its updated value will remain within the scope of the function

3. In the case of loops, values of a variable declared with var keyword get change as it is globally scoped

whereas variable declared with let keyword remain unchanged

I hope you enjoyed reading this article, as much as I enjoyed writing it. If you like this article please let me know! But, more importantly if you disagree with this article please, please, please let me know! I made this with the hope of helping the community so if it is off it defeats the purpose! If you have a suggestion or critique please feel free to drop in any comments.