Consider the following JavaScript code:
console.log(a); let a = 5;
What will be printed to the console?
console.log(a); let a = 5;
Think about the temporal dead zone for let variables.
Variables declared with let are hoisted but not initialized. Accessing them before declaration causes a ReferenceError due to the temporal dead zone.
Look at this code:
console.log(b); const b = 10;
What will be the output?
console.log(b); const b = 10;
Remember how const variables behave before initialization.
const variables are hoisted but not initialized. Accessing them before declaration causes a ReferenceError due to the temporal dead zone.
Analyze this code:
var x = 1;
{
console.log(x);
let x = 2;
}What will be printed?
var x = 1; { console.log(x); let x = 2; }
Consider the block scope and temporal dead zone of let.
The block declares a new let x which shadows the outer var x. The console.log(x) tries to access the inner x before initialization, causing a ReferenceError.
Consider this code:
function test() {
console.log(y);
const y = 3;
}
test();What will be printed when test() is called?
function test() { console.log(y); const y = 3; } test();
Think about the temporal dead zone inside functions with const.
The variable y is declared with const inside the function. Accessing it before declaration causes a ReferenceError due to the temporal dead zone.
Choose the statement that best describes how let and const variables behave during hoisting in JavaScript.
Recall the temporal dead zone concept for let and const.
Both let and const declarations are hoisted but remain uninitialized until their declaration line is executed. Accessing them before initialization causes a temporal dead zone error.