0
0
Javascriptprogramming~20 mins

Common hoisting pitfalls 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 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);
A5\n5
BReferenceError\n5
Cundefined\n5
Dundefined\nundefined
Attempts:
2 left
πŸ’‘ Hint
Remember that var declarations are hoisted but not their assignments.
❓ Predict Output
intermediate
2: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;
AReferenceError
BTypeError
C10
Dundefined
Attempts:
2 left
πŸ’‘ Hint
Variables declared with let are not accessible before their declaration.
❓ Predict Output
advanced
2: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');
};
Afoo called\nTypeError
BTypeError\nTypeError
CReferenceError\nbar called
Dfoo called\nbar called
Attempts:
2 left
πŸ’‘ Hint
Function declarations are hoisted fully, but var assigned functions are hoisted as undefined.
❓ Predict Output
advanced
2: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);
AReferenceError\n20
Bundefined\n20
Cundefined\nundefined
D20\n20
Attempts:
2 left
πŸ’‘ Hint
var declarations are function-scoped, not block-scoped.
❓ Predict Output
expert
3: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() {}
A"undefined"\n"undefined"\n"function"
B"undefined"\n"string"\n"function"
C"string"\nReferenceError\n"function"
D"undefined"\nReferenceError\n"function"
Attempts:
2 left
πŸ’‘ Hint
let variables are not accessible before declaration, causing ReferenceError even with typeof.