0

Please consider the following JavaScript code:

let funcs = []
for(let x of [1,2,3]) {
    funcs[x] = function(){
        console.log("test:", x)
    }
}

funcs[1]()
funcs[2]()
funcs[3]()

When I run this code in Google Chrome V55.0 it generates the expected output of

test: 1
test: 2
test: 3

in the browser console. However, if I run the same code in Firefox V50.1.0, I get the following console output:

test: 3
test: 3
test: 3

The link here (about a third of the way down at "Block Scope with Let") suggests that the Chrome console output is the correct one, but I would like to use the same code in both browsers. Can someone point out if I've made a mistake somewhere?

Just a student
  • 10,560
  • 2
  • 41
  • 69
d3newguy
  • 11
  • 4

0 Answers0