console.log(a);
var a = 1;
console.log(b);
let b = 2;
Logs
undefined, then throws a ReferenceErrorvar a is hoisted and set to undefined, so the first log prints undefined. let b is also hoisted, but it stays in the temporal dead zone (TDZ) until its declaration line runs. Reading it there throws ReferenceError: Cannot access 'b' before initialization.
The tempting option "Logs undefined twice" is wrong because let is not set to undefined when hoisted. Only var is.
Read more
- Hoisting (MDN) (https://developer.mozilla.org/en-US/docs/Glossary/Hoisting)developer.mozilla.org