Share via


Enumerating JavaScript properties

Due to the changes made in the JavaScript object model of Windows Internet Explorer 9, JavaScript properties may be enumerated differently from how they are enumerated in earlier versions.

When using the for…in Statement, in any document mode, the order of property enumerations may be different from the order returned by Windows Internet Explorer 8. For example, numeric properties now enumerate before non-numeric properties.

Be aware of the difference in enumeration order between Internet Explorer 8 and Internet Explorer 9; see the examples below.

The following sample illustrates the difference in enumeration order between Internet Explorer 8 and Internet Explorer 9:

var obj = {first : "prop1", second: "prop2", 3: "prop3"};

var s = "";
for (var key in obj) {
    s += key + ": " + obj[key] + " ";
}
document.write (s);

Running this sample in Internet Explorer 8 and Internet Explorer 9 would print the following results:

All modes of Internet Explorer 8:

first: prop1 second: prop2 3: prop3

All modes of Internet Explorer 9:

3: prop3 first: prop1 second: prop2

Internet Explorer 8 does not include enumerations of properties that have the same name as built-in properties of a prototype object. All document modes in Internet Explorer 9 include these properties in the enumeration. The following sample illustrates this difference:

var obj = { first: "prop1", toString : "Hello" }
var s = "";
for (var key in obj) {
    s += key + ": " + obj[key] + " ";
}
document.write (s);

Running this sample in Internet Explorer 8and Internet Explorer 9 would print the following results:

All modes of Internet Explorer 8:

first: prop1

All modes of Internet Explorer 9:

first: prop1 toString: Hello