var test = function() {
'use strict';
var mapNames = {
'name': 'City Name:',
'coord.lat': 'Latitute:'
};
for (var key in mapNames) {
var names;
if (mapNames[key]) {
name = mapNames[key];
} else {
name = key;
}
}
console.log(name);
}
test();
In the code above I made a mistake by declaring variable names
and using name
instead. I thought 'strict' mode would catch it but it didn't. Shouldn't this throw an error in this case?
var test = function() {
'use strict';
var mapNames = {
'name': 'City Name:',
'coord.lat': 'Latitute:'
};
for (var key in mapNames) {
var names;
if (mapNames[key]) {
name = mapNames[key];
} else {
name = key;
}
}
console.log(name);
}
test();
In the code above I made a mistake by declaring variable names
and using name
instead. I thought 'strict' mode would catch it but it didn't. Shouldn't this throw an error in this case?
1 Answer
Reset to default 25A name
global variable already exists, unrelated to your code; it represents the name of the current window, so you are assigning to an already existing variable.
window.name; // the name of the current window for cross-window communication
Everything on window
is declared as a global - so it is not reference-erroring since it is assigning to a variable in an outer scope.
Super confusing :D
window.name
on MDN.- HTML Specification, window name property.
"use strict"
would prevent defining new global variables, here we are performing an assignment to an existing variable, think of it as name
is in the global scope, like window.Blob
, window.console
and so on.