ES5 introduced use of let
, but I don't understand if I already have var
then why i need to use keyword let
in javascript.
Asked
Active
Viewed 34 times
1

pgbrim
- 11
- 1
-
1A simple google query would give you [this](https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var). – Jasper Ketelaar Jul 19 '20 at 09:33
-
They are different. Let addresses the weaknesses of var. – evolutionxbox Jul 19 '20 at 09:34
-
4Does this answer your question? [What's the difference between using "let" and "var"?](https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var) – evolutionxbox Jul 19 '20 at 09:34
1 Answers
0
Variables declared with let and const eliminate this specific issue of hoisting because they’re scoped to the block, not to the function. Previously, when you used var, variables were either scoped globally or locally to an entire function scope.
If a variable is declared using let or const inside a block of code (denoted by curly braces { }), then the variable is stuck in what is known as the temporal dead zone until the variable’s declaration is processed. This behavior prevents variables from being accessed only until after they’ve been declared.

Basem Sleem
- 3
- 3