Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSHint considers a for-in variable 'bad'. What does this mean?

The following code:

var things = {'foo':'bar'}
for ( thing in things ) {
  console.log(thing)
}

Consistently produces the following error in jshint:

Bad for in variable 'thing'.

I do not understand what makes the 'thing' variable 'bad' - as you can see, it is not being used anywhere else. What should I do differently to make jshint not consider this to be an error?

like image 360
mikemaccana Avatar asked May 02 '12 18:05

mikemaccana


1 Answers

They always are - if they are not declared. Try adding var if thing has not been previously declared.

for ( var thing in things ) {
  console.log(thing)
}

or

var thing;

//more code

for ( thing in things ) {
  console.log(thing)
}
like image 142
Dutchie432 Avatar answered Sep 21 '22 06:09

Dutchie432