0

When I use a for in statement, js2 mode doesn't recognize the new variable:

let foo = {bar: 1};

for(key in foo) {
    console.log(key);
}

It prints: Undeclared variable or function 'key'

2

It warns you that the key variable is undeclared, and using it like this, you leak it into the global namespace.

Try this is your browser console:

> let foo = {bar: 1};

> for(key in foo) {}

> key
=> "bar"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.