0

I'm currently reading the book Secrets of the Javascript Ninja and I do not understand the following behaviour based on an example from chapter 5 explaining how function identifiers are overwritten:

code example from the book:

console.log(typeof fun);

var fun = 3;

console.log(typeof fun);

function fun() {}

console.log(typeof fun);

Output as expected: function / number / number

console.log(typeof fun);

let fun = 3;

console.log(typeof fun);

function fun() {}

console.log(typeof fun);

But when I change the definition to let I get the error that fun has already been decleared. When the lexical environment is created why can I override var but let already blocks the definition of the function declaration?

The attached question, was also answered with "let variables are not initialized until their definition is evaluated." What's the difference between using "let" and "var"?

Timo
  • 9
  • 3
  • 2
    That was pretty much a basic reason for the introduction of `let`. – Pointy Feb 06 '22 at 13:57
  • "*let variables are not initialized until their definition is evaluated.*" - yes, but that's irrelevant here. The important part of the answer you linked is "*var will let you re-declare the same variable in the same scope while let raises a SyntaxError.*" – Bergi Feb 06 '22 at 15:56

0 Answers0