function User(name,age){
this.name = name;
this.age = age;
}
var user = new User('Daniel', 45);
document.write(user[name] + ' is ' + user[age] + ' years old!');
I know the difference between dot notation and bracket notation. When the above code is run in chrome console, Uncaught ReferenceError: age is not defined
is shown. Since I've not declared name
, it should throw error as name is not defined
as it is encountered first in the code.
I logged window.name
and window.age
only to see "" and undefined respectively.
Thinking I might have used name
in global scope before, I tested the same code after clearing browser history and also in a new browser, Firefox which I've never used before. The same error is shown in both scenarios.
Does this mean that name
is initialized to empty string by default in any browser?
After I did delete window.name;
, it gave error on name
. But why would name
be already empty string in a completely new browser. Any comments would be helpful. Thanks.