Reputation: 23
console in IE 11
console in Chrome
If I change word 'item' in the loop to 'anotherItem' like this
var obj = {
id1: 'item 1',
id2: 'item 2',
id3: 'item 3'
};
for (anotherItem in obj){
console.log(anotherItem);
}
The cycle works fine
Why IE 11 does not process word 'item'
Upvotes: 2
Views: 13225
Reputation: 361
you can use this example for old browsers. good luck.
var fruits = ["apple", "orange", "cherry"];
fruits.forEach(myFunction);
function myFunction(item, index) {
document.getElementById("demo").innerHTML += index + ":" + item + "<br>";
}
Upvotes: 0
Reputation: 1117
item
is defined as a native function in IE, and is probably read-only, and therefore is the reason you cannot change it's value.
Prior to Edge, Microsoft didn't like adhering to standards, and introduced all sorts of features that aren't in the standards. The item
function is not present in Edge.
Also, you haven't declared anotherItem
, try this:
Try this:
var obj = {
id1: 'item 1',
id2: 'item 2',
id3: 'item 3'
};
for (var anotherItem in obj){
console.log(anotherItem);
}
If you don't declare a variable with the var
keywork, and you're not in strict-mode, it will be defined as a global variable (which is not what you want). Global variables are essentially properties on the global object, and in the context of a web browser, that'd be the window
object.
Add the following to the top of your JS file to enable strict mode, and then you won't be able to make these mistakes in the first place as an exception will be thrown.
"use strict";
You can also choose to enable strict mode for specific functions, like this:
(function() {
"use strict";
// code here is in strict mode
})()
Upvotes: 9