0

since today I believe in ghosts. Can someone explain the following JS behaviour with Chrome (Version 59.0.3071.115) or Firefox (54.0 (64-bit)) ?

Why?

var name = 10;
console.log(typeof name); // string
console.log(name);        // "10"

Same with let works:

let name = 10;
console.log(typeof name); // number
console.log(name);        // 10

What I believe is, that window.name is used and it transforms any input to string. But then, why is var name not a local variable and hides the global variable?

Thank you.

Aitch
  • 1,361
  • 11
  • 21
  • I believe that window.name is the value referring to the name of the currently opened window. I think that is why is must be a string. – Tianhao Zhou Jul 27 '17 at 20:39
  • 1
    @quentin rather https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var-to-declare-a-variable – Jonas Wilms Jul 27 '17 at 20:40
  • @qentin where is my answer to `why does var not hide the global variable` ? – Aitch Jul 27 '17 at 20:41
  • @Aitch [Here.](https://stackoverflow.com/a/11444416/476) – deceze Jul 27 '17 at 20:42
  • @deceze thanks, but they say: `let me = 'go'; // globally scoped` but then it is not im my case, since `let` works – Aitch Jul 27 '17 at 20:44
  • Please read the entire thing. *"However, global variables defined with `let` will not be added as properties on the global `window` object like those defined with `var`."* – deceze Jul 27 '17 at 20:45

0 Answers0