This is just a question, didn't find the answer while searching in MDN or stackoverflow. The two statements
for (let item of array)
and for (item of array)
seems to work exactly the same. Are they equivalent (i.e. is the variable item implicitly declared with let
if nothing precedes it)?
Edit:
This is similar to this - the difference is that this specifically asks about the for..of loop, and the answers that deal with the let
keywords are towards the bottom so that may get missed. But both questions are about the same concept. The gist is below:
- If a variable is not declared explicitly within for, it is considered a global variable (if not declared before, within the same function).
- If it is declared with
var
, it is local to the function in which declared. - If it is declared with
let
, it is only local to the for block.