As MDN page says,
The scope of a variable declared with var
is its current execution context,
which is either the enclosing function or, for variables declared outside any function, global.
If you re-declare a JavaScript variable, it will not lose its value.
In this case, name
is global and it points already existing variable, window.name
.
Also, according to Window.name page,
window.name
will convert all values to their string representations by using the toString
method.
So value of name
is "[object Object]"
that is String
. You can check this by
var name = {"key":"value"};
console.log(name); // [object Object]
key
property of String
value "[object Object]"
is undefined, this is why you get undefined
.