The Difference between Var, Let & Const
Variables are containers for storing data or values. A variable must have a unique name.
There are three ways that we can declare a variable, we can declare with Var, Let or Const.
JavaScript uses reserved keyword var to declare a variable, var is function scoped and a global variable which is accessible anywhere with in our program and it can be re-assigned, re-defined and can be hoisted.
e.g:-
var counter = 0
counter = 1
=> 1
One of the features that came with ECMAScript 6(ES6) is the addition of let and const, which can be used for variable declaration.
let is a Local Variable and it is block scoped and just like var it can be re-assigned but cannot be re-defined or hoisted.
e.g:-
let counter = 0
counter = 1
=> 1
const Is also a local variable and block scoped however unlike var and let, const cannot be re-assigned it also cannot be re-defined and hoisted.
e.g:-
const name = “pat”
name
=> “pat”
name = “bet”
*** Uncaught TypeError: Assignment to constant variable.***
N:B:- It is strongly advisable to use let and const over var as var is considered as a bad practice.