Challenge - 5 Problems
Hoisting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
What is the output of this code involving hoisting?
Consider this JavaScript code snippet. What will it print to the console?
Javascript
console.log(a); var a = 5;
Attempts:
2 left
π‘ Hint
Think about how variable declarations are handled before code runs.
β Incorrect
In JavaScript, variable declarations with var are hoisted to the top, but their assignments are not. So 'a' is declared but undefined at the time of console.log.
β Predict Output
intermediate2:00remaining
What will this function print due to hoisting?
Look at this code. What is the output when calling foo()?
Javascript
function foo() { console.log(bar); var bar = 'hello'; } foo();
Attempts:
2 left
π‘ Hint
Remember that variable declarations are hoisted but assignments are not.
β Incorrect
Inside the function, 'bar' is declared but not assigned before console.log, so it prints undefined.
β Predict Output
advanced2:00remaining
What is the output of this code with let and hoisting?
What will this code print to the console?
Javascript
console.log(x); let x = 10;
Attempts:
2 left
π‘ Hint
Variables declared with let are not hoisted like var.
β Incorrect
Variables declared with let are in a temporal dead zone before initialization, causing a ReferenceError if accessed early.
β Predict Output
advanced2:00remaining
What will this code output with function hoisting?
What does this code print when run?
Javascript
foo(); function foo() { console.log('Hi'); }
Attempts:
2 left
π‘ Hint
Function declarations are hoisted completely.
β Incorrect
Function declarations are hoisted with their body, so foo() can be called before its definition.
β Predict Output
expert2:00remaining
What is the output of this tricky hoisting example?
What will this code print to the console?
Javascript
console.log(typeof foo); var foo = 'bar'; function foo() {}
Attempts:
2 left
π‘ Hint
Function declarations are hoisted before variable declarations.
β Incorrect
Function declarations are hoisted before var declarations, so typeof foo is 'function' at the time of console.log.