0

I find an example in w3school like this:

carName = "Saab";
let carName = "Volvo";

So after compile it would be like this:

let carName;
carName = "Saab";
carName = "Volvo";

Why is this wrong?

Arnav Thorat
  • 3,078
  • 3
  • 8
  • 33
David Tran
  • 29
  • 4
  • 1
    `So after compile it would be like this:` are you sure? you'd actually get an error with that code ... something like `ReferenceError: can't access lexical declaration 'carName' before initialization` - so, your question makes absolutely no sense – Bravo Jun 05 '22 at 08:13
  • 1
    `let` and `const` are hoisted, but with at temporal dead zone. – t.niese Jun 05 '22 at 08:14
  • Thanks, I got it now! Silly me asking a question for such ez problem. – David Tran Jun 05 '22 at 08:18
  • Variables declared with let and const are also hoisted but, unlike var , are not initialized with a default value. An exception will be thrown if a variable declared with let or const is read before it is initialized. – Robert Jun 05 '22 at 08:18

0 Answers0