0
0
Javascriptprogramming~5 mins

Variable hoisting behavior in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is variable hoisting in JavaScript?
Variable hoisting is JavaScript's behavior of moving variable declarations to the top of their containing scope during the compilation phase, before code execution.
Click to reveal answer
intermediate
How does hoisting differ between var, let, and const?
var declarations are hoisted and initialized with undefined. let and const are hoisted but not initialized, causing a 'temporal dead zone' until their declaration is reached.
Click to reveal answer
beginner
What happens if you access a let variable before its declaration?
Accessing a let variable before its declaration throws a ReferenceError because it is in the temporal dead zone until the declaration line is executed.
Click to reveal answer
beginner
Consider this code:<br>
console.log(x); var x = 5;
<br>What is the output and why?
The output is undefined. The declaration var x is hoisted to the top and initialized with undefined, but the assignment = 5 happens after the console.log.
Click to reveal answer
intermediate
Does function declaration hoisting behave the same as variable hoisting?
Function declarations are hoisted completely, meaning the entire function body is moved to the top, allowing you to call the function before its declaration in the code.
Click to reveal answer
What will this code output?<br>
console.log(a); let a = 10;
A10
Bnull
Cundefined
DReferenceError
Which keyword's variables are hoisted and initialized with undefined?
Avar
Bconst
Clet
Dfunction
What is the temporal dead zone?
AThe time after a variable is declared
BA zone where variables are automatically initialized
CThe time before a variable is declared where it cannot be accessed
DA special memory area for variables
What will this code output?<br>
foo(); function foo() { console.log('Hello'); }
AReferenceError
BHello
Cundefined
DTypeError
Which of these is NOT true about const variables and hoisting?
A<code>const</code> variables can be accessed before declaration without error
B<code>const</code> variables cause a temporal dead zone
C<code>const</code> variables are hoisted but not initialized
D<code>const</code> variables must be initialized at declaration
Explain variable hoisting in JavaScript and how it affects var, let, and const declarations.
Think about when variables become available in the code and what happens if you try to use them too early.
You got /4 concepts.
    Describe how function declarations are hoisted and how this differs from variable hoisting.
    Consider what happens when you call a function before it appears in the code.
    You got /3 concepts.