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:
console.log(x); var x = 5;
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?
console.log(a); let a = 10;
A10
Bnull
Cundefined
DReferenceError
✗ Incorrect
Accessing a let variable before declaration causes a ReferenceError due to the temporal dead zone.
Which keyword's variables are hoisted and initialized with undefined?
Avar
Bconst
Clet
Dfunction
✗ Incorrect
var variables are hoisted and initialized with undefined.
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
✗ Incorrect
The temporal dead zone is the period from the start of a scope until a let or const variable is declared, during which accessing it causes an error.
What will this code output?
foo(); function foo() { console.log('Hello'); }
AReferenceError
BHello
Cundefined
DTypeError
✗ Incorrect
Function declarations are hoisted completely, so foo can be called before its declaration.
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
✗ Incorrect
const variables cannot be accessed before declaration; doing so causes a ReferenceError.
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.
Practice
(1/5)
1. What happens to a var variable declaration in JavaScript before code execution?
easy
A. It is not moved and must be declared before use.
B. It causes a syntax error if used before declaration.
C. It is moved to the top and initialized as undefined.
D. It is moved to the top and initialized with its assigned value.
Solution
Step 1: Understand var hoisting behavior
JavaScript moves var declarations to the top of their scope and initializes them as undefined.
Step 2: Check initialization timing
The variable is not assigned its value until the code runs the assignment line, so before that it is undefined.
Final Answer:
It is moved to the top and initialized as undefined. -> Option C
Quick Check:
var hoisted = undefined [OK]
Hint: Remember: var is hoisted and set to undefined first [OK]
Common Mistakes:
Thinking var is not hoisted
Assuming var is initialized with assigned value immediately
Confusing var with let or const hoisting
2. Which of the following is the correct way to declare a variable that is hoisted but causes a ReferenceError if accessed before declaration?
easy
A. var x = 5;
B. let x = 5;
C. function x() {}
D. const x;
Solution
Step 1: Identify hoisting behavior of let
let declarations are hoisted but not initialized, so accessing them before declaration causes a ReferenceError.
Step 2: Check other options
var is hoisted and initialized as undefined, so no ReferenceError; const must be initialized at declaration, so const x; is a syntax error; function declarations are hoisted differently.
Final Answer:
let x = 5; -> Option B
Quick Check:
let hoisted but uninitialized causes ReferenceError [OK]
Hint: let and const cause errors if used before declaration [OK]
Common Mistakes:
Using const without initialization
Confusing var and let hoisting
Thinking function declarations behave like let
3. What is the output of the following code?
console.log(a);
var a = 10;
console.log(a);
medium
A. undefined 10
B. 10 10
C. ReferenceError undefined
D. undefined undefined
Solution
Step 1: Analyze var hoisting for variable a
The declaration var a is hoisted and initialized as undefined before any code runs.
Step 2: Trace console.log outputs
The first console.log(a) prints undefined because assignment hasn't happened yet. After a = 10, the second console.log(a) prints 10.
Final Answer:
undefined 10 -> Option A
Quick Check:
var hoisted as undefined, then assigned [OK]
Hint: var logs undefined before assignment, then assigned value [OK]
Common Mistakes:
Expecting ReferenceError on first log
Thinking var is assigned before declaration
Confusing let/const behavior with var
4. Find the error in the following code and choose the correct fix:
console.log(b);
let b = 20;
medium
A. Change let to var to avoid ReferenceError.
B. Wrap the code in a function to fix hoisting.
C. Initialize b with a value before logging.
D. Move console.log(b) after the declaration.
Solution
Step 1: Identify cause of ReferenceError
let variables are hoisted but not initialized, so accessing b before declaration causes ReferenceError.
Step 2: Fix by reordering code
Moving console.log(b) after let b = 20; ensures b is declared before use, avoiding the error.
Final Answer:
Move console.log(b) after the declaration. -> Option D
Quick Check:
Access let after declaration to avoid error [OK]
Hint: Use variables only after let/const declaration [OK]
Common Mistakes:
Changing let to var without understanding scope
Trying to initialize before declaration (syntax error)
Assuming wrapping in function fixes hoisting
5. Consider this code:
console.log(x);
if (true) {
var x = 5;
let y = 10;
console.log(y);
}
console.log(x);
console.log(y);
What will be the output and why?
hard
A. undefined 10 5 ReferenceError
B. ReferenceError 10 5 ReferenceError
C. undefined 10 5 10
D. undefined ReferenceError 5 ReferenceError
Solution
Step 1: Analyze var x hoisting and scope
var x is hoisted to the function or global scope and initialized as undefined. So, first console.log(x) prints undefined. After assignment inside the block, x becomes 5.
Step 2: Analyze let y block scope and access
let y = 10 is block scoped. Inside the block, console.log(y) prints 10. Outside the block, console.log(y) causes ReferenceError because y is not defined there.
Step 3: Trace remaining console.logs
After the block, console.log(x) prints 5. The last console.log(y) throws ReferenceError.
Final Answer:
undefined 10 5 ReferenceError -> Option A
Quick Check:
var hoisted globally, let block scoped [OK]
Hint: var is function/global scoped; let is block scoped [OK]
Common Mistakes:
Assuming let is accessible outside block
Expecting var to be block scoped
Confusing initialization timing of var and let
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.