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
beginner
How does hoisting affect variable declarations with var?
Variables declared with var are hoisted and initialized with undefined, so you can use them before their declaration without a ReferenceError, but their value will be undefined until assigned.
Click to reveal answer
intermediate
Are let and const variables hoisted in JavaScript?
Yes, let and const declarations are hoisted but not initialized. Accessing them before declaration causes a ReferenceError due to the 'temporal dead zone'.
Click to reveal answer
beginner
How are function declarations treated during hoisting?
Function declarations are fully hoisted, meaning the entire function is moved to the top of the scope, so you can call the function before its declaration in the code.
Click to reveal answer
intermediate
What happens if you try to use a function expression before it is defined?
Function expressions assigned to variables are hoisted only as variable declarations (with var hoisted as undefined). Calling the function before assignment results in an error because the variable is not yet a function.
Click to reveal answer
What does hoisting do in JavaScript?
AMoves declarations to the top of their scope before execution
BRuns code faster by optimizing loops
CPrevents variables from being used before declaration
DAutomatically initializes variables with default values
✗ Incorrect
Hoisting moves variable and function declarations to the top of their scope before the code runs.
What value does a var variable have if accessed before its declaration?
Aundefined
BThe assigned value
Cnull
DReferenceError
✗ Incorrect
var variables are hoisted and initialized with undefined.
What happens if you access a let variable before its declaration?
AReturns undefined
BReturns null
CThrows ReferenceError
DReturns 0
✗ Incorrect
let variables are hoisted but not initialized, causing a ReferenceError if accessed early.
Can you call a function declared with function keyword before its declaration?
ANo, it causes a ReferenceError
BYes, because function declarations are hoisted
COnly if declared with <code>var</code>
DOnly if declared with <code>let</code>
✗ Incorrect
Function declarations are fully hoisted, so you can call them before their code line.
What happens if you call a function expression assigned to a var variable before assignment?
AFunction runs normally
BReturns undefined
CThrows ReferenceError
DThrows TypeError
✗ Incorrect
The variable is hoisted as undefined, so calling it as a function causes a TypeError.
Explain hoisting in JavaScript and how it affects variables declared with var, let, and const.
Think about where declarations move before code runs and how accessing variables early behaves.
You got /4 concepts.
Describe the difference between hoisting of function declarations and function expressions.
Consider how the entire function or just the variable name moves up.
You got /4 concepts.
Practice
(1/5)
1. What does hoisting mean in JavaScript?
easy
A. JavaScript converts all variables to global scope automatically.
B. JavaScript moves variable and function declarations to the top before running code.
C. JavaScript delays execution of code until user interaction.
D. JavaScript runs code only after the entire file is loaded.
Solution
Step 1: Understand hoisting concept
Hoisting means JavaScript moves declarations of variables and functions to the top of their scope before running the code.
Step 2: Compare options
Only JavaScript moves variable and function declarations to the top before running code. correctly describes this behavior. Other options describe unrelated behaviors.
Final Answer:
JavaScript moves variable and function declarations to the top before running code. -> Option B
Quick Check:
Hoisting = Moving declarations up [OK]
Hint: Hoisting means declarations move up before code runs [OK]
Common Mistakes:
Thinking hoisting moves variable assignments, not just declarations
Confusing hoisting with asynchronous code execution
Believing hoisting applies to all variable types equally
2. Which of the following variable declarations is hoisted and initialized as undefined?
easy
A. function foo() {}
B. const y = 10;
C. let x = 5;
D. var z = 15;
Solution
Step 1: Identify hoisting behavior for variable types
Variables declared with var are hoisted and initialized as undefined. let and const are hoisted but not initialized, causing a temporal dead zone.
Step 2: Match options to behavior
var z = 15; uses var, so it is hoisted and initialized as undefined. Others do not behave this way.
Final Answer:
var z = 15; -> Option D
Quick Check:
var hoisted as undefined [OK]
Hint: Only var variables are hoisted and set to undefined [OK]
Common Mistakes:
Assuming let and const behave like var with hoisting
Confusing function declarations with variable declarations
Thinking function expressions are hoisted like functions
3. What will be the output of this code?
console.log(myVar);
var myVar = 10;
medium
A. undefined
B. 10
C. ReferenceError
D. null
Solution
Step 1: Understand var hoisting with initialization
The declaration var myVar is hoisted to the top, but the assignment = 10 stays in place.
Step 2: Evaluate console.log output
At the time of console.log(myVar), myVar exists but is undefined because assignment hasn't happened yet.
Final Answer:
undefined -> Option A
Quick Check:
var declaration hoisted, value undefined [OK]
Hint: var declaration hoisted, value assigned later [OK]
Common Mistakes:
Expecting the value 10 to print immediately
Confusing ReferenceError with undefined for var
Thinking let or const behave the same way
4. Find the error in this code related to hoisting:
console.log(foo());
var foo = function() { return 'Hello'; };
medium
A. No error, outputs 'Hello'
B. SyntaxError
C. TypeError: foo is not a function
D. ReferenceError: foo is not defined
Solution
Step 1: Understand hoisting of function expressions
Variable foo is hoisted as undefined, but the function assignment is not hoisted.
Step 2: Analyze calling foo before assignment
Calling foo() before assignment tries to call undefined as a function, causing a TypeError.
Final Answer:
TypeError: foo is not a function -> Option C
Quick Check:
Function expressions not hoisted as functions [OK]
Hint: Function expressions hoisted as undefined, not callable [OK]
Common Mistakes:
Thinking function expressions are fully hoisted
Expecting no error because of hoisting
Confusing ReferenceError with TypeError
5. Consider this code:
console.log(greet());
function greet() {
return 'Hi!';
}
var greet = function() {
return 'Hello!';
};
console.log(greet());
What will be the output?
hard
A. 'Hi!' then ReferenceError
B. 'Hello!' then 'Hello!'
C. ReferenceError then 'Hello!'
D. 'Hi!' then 'Hello!'
Solution
Step 1: Analyze hoisting of function declaration
The function declaration function greet() is fully hoisted, so the first greet() call returns 'Hi!'.
Step 2: Analyze var assignment after first call
The var greet declaration is hoisted but initialized as undefined. The assignment to a function expression happens after the first call, so the second greet() call uses the new function returning 'Hello!'.
Final Answer:
'Hi!' then ReferenceError -> Option A
Quick Check:
Function declaration hoisted, var assignment overrides later [OK]
Hint: Function declarations hoisted fully; var assignments override later [OK]
Common Mistakes:
Thinking var assignment overrides before first call
Expecting ReferenceError due to var redeclaration
Confusing function declaration and expression hoisting
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.