I am creating a very basic object in JavaScript and looping thru its properties, displaying property name:
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 expected result:
A B C D E
But in Chrome same code produces
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Any idea why? Does keyword name
hold some significance in Chrome?
The for...in statement iterates over all enumerable string properties of an object (ignoring properties keyed by symbols), including inherited enumerable properties.
Chrome doesn't seem to like it when you use it as a global variable (there's also a name property on the window object). Just run it inside a function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With