Javascript – IE8 will report error about for in without declaring var item

for-loopinternet-explorer-8javascript

Like :

var result = eval('(' + response + ')');
var html = value = '';

for(item in result)
{

}

response is a json response.

It stops at for.. in IE8.

How to fix that?

EDIT

I got the same error when running:

result = [1,2,3];
for(item in result)
{
...
}

Best Answer

I tested the code from JavaScript For...In Statement in IE8, no issue.

Definitely not an issue of the loop (not working in IE8) but what is in the 'result' object.

UPDATE:

I found the issue.

In IE8 (not sure about other IE versions) the word "item" somehow is a reserved word or something.

This will work:

var item;
for(item in result)
{
...
}

This will not (if item is not declared):

for(item in result)
{
...
}

This will work:

for(_item in result)
{
...
}