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

let foo = {bar: 1};

for(key in foo) {

It prints: Undeclared variable or function 'key'

1 Answer 1


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 and acknowledge that you have read and understand our privacy policy and code of conduct.

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