1

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:

  1. If a variable is not declared explicitly within for, it is considered a global variable (if not declared before, within the same function).
  2. If it is declared with var, it is local to the function in which declared.
  3. If it is declared with let, it is only local to the for block.
mohitb
  • 151
  • 2
  • 11
  • 1
    It would likely depend on which version of JavaScript/EMCAScript you're targeting, and whether you've enabled strict mode. – Tieson T. Jul 19 '19 at 06:03
  • `let` explicitely declares a block-scoped variabe. Without, the loop still iterates a variable `item` but it assumes that it has been declared beforehand. If not, it creates a global variable or crashes in strict mode. – Thomas Jul 19 '19 at 06:03
  • `let` has blocked scope, so `item` can only be used within the for loop, `item` alone isn't defined to the scope of the loop itself: https://jsfiddle.net/e17Lkwx0/ (there are more differences though (see [this](https://stackoverflow.com/a/35812376/5648954))) – Nick Parsons Jul 19 '19 at 06:04
  • Also, you might find this to be a useful read: https://www.sitepoint.com/how-to-declare-variables-javascript/ – Tieson T. Jul 19 '19 at 06:05
  • 1
    Read about the [`let` keyword](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Variables#The_difference_between_var_and_let) in JavaScript. – axiac Jul 19 '19 at 06:15

2 Answers2

4

If you do not use let or var in the loop, you create a variable in the global scope

The var stays defined only in the function

The let stays defined only in the loop statement

const arr = ["One","Two","Three"]
function test1() {
  for (var a of arr) console.log(a);
  console.log("var a after loop:",a) // works
}  

function test2() {
  for (let b of arr) console.log(b);
  console.log("trying to log `let b` after loop")
  try {
    console.log("let b after loop:",b); // fails  
  }
  catch(e) { console.log("Error:",e.message)}
}  

function test3() {
  for (c of arr) console.log(c);
  console.log("'global' c after loop:",c) //works 
}  


test1(); 
test2(); 
test3(); 

// fails
console.log("Trying to log var a after function")
try { console.log("var a after function:",a);}
catch(e) { console.log("Error:",e.message)}  
console.log("Trying to log let b after function")
try { console.log("b after function:",b);}
catch(e) { console.log("Error:",e.message)}

console.log("'global' c after function:",c); // logs
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

They are equivalent functionally. But this is not a good practice to do so. Make sure to use let or var as it will prevent you from getting weird bugs or issues if your code gets large, as simply using item is creating or referring to a global variable. It is a time bomb

dp2050
  • 332
  • 3
  • 8