According to the first image, we can find that i has been declared ,why we can use 'let' to declare it again?
Asked
Active
Viewed 35 times
-3
-
1post your code instead of the image? – prasanth May 09 '17 at 05:43
-
Because block scopes. – Bergi May 09 '17 at 05:45
-
In first case you don't have to declare i twice.however you can re-assign values. In second case i is declared in two different scopes(as let is block scoped). – Manish May 09 '17 at 05:47
-
Have a look at `{ let i=0; { let i=1; }}`. Now try to omit parenthesis or replace a `let` with `var`. – Bergi May 09 '17 at 05:53
-
This Link can help you to differ let and var :http://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var-to-declare-a-variable – RRajani May 09 '17 at 05:55
-
Thank,i have found where is the problem. – Messiah May 09 '17 at 06:08
1 Answers
0
OK. This is something about scopes.
Var is a function-based keyword. The variable you declare with a var instruction will be available for the whole function.
Let is a block-based keyword. The variable will be available for the current scope only. (Scope are most of the time delimited with {}. )
So in your first example, you tried to define a variable with var. But it has already been declared for the function.
In the second example however, you can see that the variable is defined for another scope that the one you are using (for inner scope, see the {} after the for instruction.) .

Kuu Aku
- 320
- 2
- 6