Iterating through object properties gives different results in different browsers

I create a very simple object in JavaScript and iterate over its properties by displaying the name of the property:

var name = { 'A': 'DataA', 'B': 'DataB', 'C': 'DataC', 'D': 'DataD', 'E': 'DataE' } for (var propName in name) { document.getElementById('result').innerHTML += propName + ' ' } 

In IE and FireFox, it produces the expected result:

 ABCDE 

But in Chrome, the same code produces

 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 

Any idea why? Does the value of the name keyword have some meaning in Chrome?

+7
source share
2 answers

Chrome doesn't seem to like it when you use it as a global variable (it also has a name property in the window object). Just run it inside the function.

+4
source

try

 for (var propName in name) { document.getElementById('result').innerHTML += (propName + ' '); } 
-5
source

All Articles