0
var a = [];
var b = ['abc','def'];
for(var c of b) {
  var c_ = c.slice();
  a.push(function(){alert(c_)});
}

a[0]();
a[1]();

Result is seing the message "def" twice. This is entirely unexpected. How do I do this in JavaScript? If it were alert(c) I could understand but c_ is clearly a copy of c and is properly scoped by other language's standards.

mroman
  • 1,354
  • 9
  • 14

1 Answers1

0

var is always function scoped. let is block scoped!.

mroman
  • 1,354
  • 9
  • 14