0
0
Javascriptprogramming~5 mins

Hoisting with let and const in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is hoisting in JavaScript?
Hoisting is JavaScript's behavior of moving variable and function declarations to the top of their containing scope before code execution.
Click to reveal answer
intermediate
How does hoisting behave differently with let and const compared to var?
let and const are hoisted but not initialized. They stay in a 'temporal dead zone' until their declaration line, causing a ReferenceError if accessed earlier. var is hoisted and initialized with undefined.
Click to reveal answer
beginner
What happens if you try to access a let variable before its declaration?
You get a ReferenceError because the variable is in the temporal dead zone until its declaration is reached.
Click to reveal answer
beginner
Can you reassign a const variable after declaration?
No, const variables cannot be reassigned after they are initialized. Trying to do so causes a TypeError.
Click to reveal answer
intermediate
Explain the 'temporal dead zone' in simple terms.
The temporal dead zone is the time between entering a scope and when a let or const variable is declared. During this time, the variable exists but cannot be accessed.
Click to reveal answer
What will happen if you run this code?<br>
console.log(x);<br>let x = 5;
AReferenceError
Bundefined
C5
DTypeError
Which statement is true about const variables?
A<code>const</code> variables can be reassigned anytime.
B<code>const</code> variables are hoisted but not initialized, causing a temporal dead zone.
C<code>const</code> variables behave exactly like <code>var</code>.
D<code>const</code> variables are hoisted and initialized with undefined.
What is the temporal dead zone?
AThe time when variables are accessible before declaration.
BA special block where variables are permanently undefined.
CThe time after a variable is declared and initialized.
DThe time between entering a scope and the declaration of <code>let</code> or <code>const</code> variables.
What will this code output?<br>
var a = 1;<br>console.log(a);<br>var a = 2;<br>console.log(a);
A1 then 2
Bundefined then 2
CReferenceError
D2 then 2
Which keyword allows hoisting with initialization to undefined?
Alet
Bconst
Cvar
Dfunction
Describe how hoisting works differently for var, let, and const variables.
Think about when variables become usable in the code.
You got /4 concepts.
    Explain the concept of the temporal dead zone and why it matters when using let and const.
    Consider what happens if you try to use a variable too early.
    You got /4 concepts.