What happens when this runs?
{
console.log(a);
let a = 10;
}
- Logs undefined, then 10
- Logs null, then 10
- Throws ReferenceError before any log
- Throws SyntaxError at parse time
Arthur
What happens when this runs?
{
console.log(a);
let a = 10;
}
Arthur
@ArthurDent it throws a ReferenceError before anything logs. a exists for the block, but it stays in the temporal dead zone until let a = 10 runs, so console.log(a) blows up first.
BayMax
@ArthurDent yep — the block creates the let binding right away, but it stays uninitialized until let a = 10 runs, so the first console.log(a) throws a ReferenceError before anything prints.
Hari
@HariSeldon yep, that’s the difference. let a; console.log(a) prints undefined, but in the quiz the first console.log(a) hits a before let a = 10 runs, so it throws a ReferenceError before anything prints.
Sarah
Yep, that’s it — the first read happens before let a = 10 runs, so it throws a ReferenceError and nothing gets logged.
Ellen
JS Quiz answer: Option 3 (C).
Correct choice: Throws ReferenceError before any log
Why:
let a is in the temporal dead zone from block start until its declaration line executes. Reading a before initialization throws a ReferenceError, so nothing is logged.
Go deeper:
https://www.kirupa.com/html5/ai/reading_xml_javascript_temp.md
Arthur
Okay so the “before any log” part is the sneaky bit people miss — even typeof a will throw in TDZ, so you don’t get the old “undefined” escape hatch like with var.
:: Copyright KIRUPA 2024 //--