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 with function declarations and calls?
Consider the following JavaScript code. What will be printed to the console when it runs?
Javascript
console.log(foo()); function foo() { return 'Hello from foo'; }
Attempts:
2 left
π‘ Hint
Function declarations are hoisted completely, so they can be called before their definition.
β Incorrect
In JavaScript, function declarations are hoisted with their body, so calling foo() before its declaration works fine and returns the string.
β Predict Output
intermediate2:00remaining
What happens when calling a function expression before its definition?
Look at this code snippet. What will be the result when it runs?
Javascript
console.log(bar()); var bar = function() { return 'Hello from bar'; };
Attempts:
2 left
π‘ Hint
Variable declarations are hoisted but not their assignments.
β Incorrect
The variable bar is hoisted but initialized as undefined at the time of the call, so calling bar() causes a TypeError because undefined is not a function.
β Predict Output
advanced2:00remaining
What is the output when a function declaration and variable share the same name?
Analyze this code and determine what it prints to the console.
Javascript
console.log(baz); var baz = 5; function baz() { return 10; } console.log(typeof baz);
Attempts:
2 left
π‘ Hint
Function declarations are hoisted before variable declarations, but variable assignments override the function.
β Incorrect
The function baz is hoisted first, so the first console.log prints the function representation. The var baz = 5 then performs the assignment, overriding baz with 5. Thus, typeof baz is 'number'.
β Predict Output
advanced2:00remaining
What error occurs when calling a let-declared function before its definition?
What happens when this code runs?
Javascript
console.log(qux()); let qux = function() { return 'Hello from qux'; };
Attempts:
2 left
π‘ Hint
Variables declared with let are not hoisted like var and cannot be accessed before initialization.
β Incorrect
Using let means the variable is in a temporal dead zone until initialized, so accessing qux before assignment throws a ReferenceError.
π§ Conceptual
expert2:00remaining
Which option correctly explains function hoisting behavior in JavaScript?
Select the statement that best describes how function declarations and function expressions are hoisted in JavaScript.
Attempts:
2 left
π‘ Hint
Think about how JavaScript treats function declarations differently from variables assigned functions.
β Incorrect
Function declarations are hoisted completely, so they can be called before their definition. Function expressions assigned to variables are hoisted only as variable declarations, so the variable is undefined before assignment.