0

I properly understand differences between let and var keywords, where and where they should be used, but in the code here i am stumped when i get this error.

Why does the following code work fine with Let keyword but gives error with var keyword..

let name = function () {
    console.log('my name is Sid');
}

let age = function (str1) {

    str1();

}

age(name);

this gives an error: Uncaught TypeError: str1 is not a function

var name = function () {
    console.log('my name is Sid');
}

var age = function (str1) {

    str1();

}

age(name);
sid
  • 365
  • 2
  • 11
  • 5
    You're trying to override `window.name`; it's a browser weirdness. – Pointy Aug 09 '19 at 16:00
  • when using var you are using the window level scope, and window.name is already defined. When using let you are tightening the scope and that allows the "name" variable to be used. – Steve Bohmbach Aug 09 '19 at 16:10
  • so you mean window.name is reserved by the browser? – sid Aug 09 '19 at 16:13

0 Answers0