0
0
Javascriptprogramming~20 mins

Variable hoisting behavior in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Hoisting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of var hoisting in function scope
What is the output of this JavaScript code?
Javascript
function test() {
  console.log(x);
  var x = 5;
  console.log(x);
}
test();
Aundefined\nundefined
B5\n5
CReferenceError: x is not defined
Dundefined\n5
Attempts:
2 left
πŸ’‘ Hint
Remember that var declarations are hoisted but not their assignments.
❓ Predict Output
intermediate
2:00remaining
Output of let hoisting in block scope
What happens when this code runs?
Javascript
console.log(a);
let a = 10;
A10
BReferenceError: Cannot access 'a' before initialization
Cundefined
Dnull
Attempts:
2 left
πŸ’‘ Hint
Variables declared with let are hoisted but not initialized immediately.
❓ Predict Output
advanced
2:00remaining
Output of var and function hoisting combined
What is the output of this code snippet?
Javascript
console.log(foo);
var foo = 1;
function foo() {}
console.log(foo);
A[Function: foo]\n1
B1\n1
Cundefined\n1
DReferenceError: foo is not defined
Attempts:
2 left
πŸ’‘ Hint
Function declarations are hoisted before var declarations.
❓ Predict Output
advanced
2:00remaining
Output of const hoisting and temporal dead zone
What will this code output or error?
Javascript
console.log(bar);
const bar = 3;
ATypeError: Assignment to constant variable.
Bundefined
CReferenceError: Cannot access 'bar' before initialization
D3
Attempts:
2 left
πŸ’‘ Hint
Const variables behave like let in hoisting and temporal dead zone.
🧠 Conceptual
expert
2:00remaining
Why does this code produce 'undefined' instead of error?
Consider this code:
console.log(x);
var x = 7;

Why does it print undefined instead of throwing an error?
ABecause var declarations are hoisted and initialized with undefined before code runs
BBecause the assignment happens before the console.log
CBecause console.log ignores errors for var variables
DBecause JavaScript treats undeclared variables as undefined automatically
Attempts:
2 left
πŸ’‘ Hint
Think about what hoisting means for var variables.