-3

enter image description here

enter image description here

According to the first image, we can find that i has been declared ,why we can use 'let' to declare it again?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Messiah
  • 63
  • 5

1 Answers1

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