Challenge - 5 Problems
Hoisting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
Output of var hoisting with function scope
What is the output of this JavaScript code?
console.log(x); var x = 5; console.log(x);
Javascript
console.log(x); var x = 5; console.log(x);
Attempts:
2 left
π‘ Hint
Remember that var declarations are hoisted but not their assignments.
β Incorrect
The variable declaration 'var x' is hoisted to the top, so the first console.log sees 'x' declared but not assigned, resulting in 'undefined'. After assignment, the second console.log prints 5.
β Predict Output
intermediate2:00remaining
let variable temporal dead zone
What happens when this code runs?
console.log(y); let y = 10;
Javascript
console.log(y); let y = 10;
Attempts:
2 left
π‘ Hint
Variables declared with let are not accessible before their declaration.
β Incorrect
Variables declared with let are hoisted but not initialized, causing a temporal dead zone. Accessing 'y' before declaration throws ReferenceError.
β Predict Output
advanced2:00remaining
Function declaration hoisting vs function expression
What is the output of this code?
foo();
bar();
function foo() {
console.log('foo called');
}
var bar = function() {
console.log('bar called');
};Javascript
foo(); bar(); function foo() { console.log('foo called'); } var bar = function() { console.log('bar called'); };
Attempts:
2 left
π‘ Hint
Function declarations are hoisted fully, but var assigned functions are hoisted as undefined.
β Incorrect
The function 'foo' is hoisted completely, so calling it works. The variable 'bar' is hoisted but assigned undefined initially, so calling bar() causes TypeError.
β Predict Output
advanced2:00remaining
var inside block scope and hoisting
What will this code output?
console.log(a);
if (true) {
var a = 20;
}
console.log(a);Javascript
console.log(a); if (true) { var a = 20; } console.log(a);
Attempts:
2 left
π‘ Hint
var declarations are function-scoped, not block-scoped.
β Incorrect
The var 'a' is hoisted to the top of the function or global scope, so the first console.log prints undefined. After assignment inside the block, the second console.log prints 20.
β Predict Output
expert3:00remaining
Complex hoisting with var, let, and function declarations
What is the output of this code?
console.log(typeof foo);
console.log(typeof bar);
console.log(typeof baz);
var foo = 'foo';
let bar = 'bar';
function baz() {}
Javascript
console.log(typeof foo); console.log(typeof bar); console.log(typeof baz); var foo = 'foo'; let bar = 'bar'; function baz() {}
Attempts:
2 left
π‘ Hint
let variables are not accessible before declaration, causing ReferenceError even with typeof.
β Incorrect
var foo is hoisted and initialized as undefined, so typeof foo is 'undefined'. let bar is in temporal dead zone, so typeof bar throws ReferenceError. function baz is hoisted fully, so typeof baz is 'function'.