ES6 Declaration of Variables
1 min readFeb 1, 2024
There are three ways of declaration of variables using ‘var’, ‘let’, and ‘const’.
Var
- Variables declared with ‘var’ are function scoped they are accessible within the functions only.
- They are hoisted on the top of the scope.
- They can be re-defined and re-declared and can update the value.
var a = 10;
Let
- Variables declared with ‘let’ are block-scoped meaning they are accessible within the curly braces they are defined.
- They can not be hoisted on the top.
- They can not be re-declared and they can be updated.
let b= 30;
Const
- Variables declared with ‘const’ are block-scoped.
- They can not be reassigned.
- However Objects are arrays are declared with const for it; the properties and elements can be modified.
Const z = 50;
So, in ES6 there are total three ways to define variables in ES6 ‘var’, ‘let’ and ‘const’.