Javascript understand the difference between let vs var Easily
The difference is scoping. var
is scoped to the nearest function block and let
is scoped to the nearest enclosing block which can be smaller than a function block.
Both are global if outside any block.
So variable declares within a function can be accessed anywhere in the function.
You cant access variable declared with var outside of function because var is just global to its function level not outside of the function
Also, variables declared with
let
are not accessible before they are declared in their enclosing block.
Important
- You can’t redeclare variable using let but using var you can redeclare the same variable
2. If var, let declare in Global scope :
3. Block-level (let) and Function-level(var) scope Example
Thanks for reading please give a clap if you like this post and follow me for more articles.