Var, Let, and Const in JavaScript
Types of variables
- Var --> function scoped
- Let --> block scoped
- Const --> block scoped
Var
When a Var variable is declared outside the function , it becomes globally scoped .
If they are inside a function we only can access them inside the function.
goodbye variable isfunction-scoped as it's limited to that function only. An example of the same is given belowπππ One of the issues with using the var keyword is they can be redeclared inside the same scope.
Let
let variables are block-scoped.also , they can not be redaclared. Like var, a let variable and its value can be updated in the same scope. but, we canβt redeclare a let variable.
It is generally used inside the loops & conditional blocks .πππ
Const
The variables associated with the const are the block scoped variable.
They can be only accessed within the block of code.
Const variables cannot be updated or redeclared. πππ
Connect With Me!
Twitter: twitter.com/omkarBorude
LinkedIn: linkedin.com/in/omkar-borude-b4583016b
GitHub: github.com/omkarborude