0
0
Javascriptprogramming~20 mins

Function 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
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';
}
AReferenceError: foo is not defined
B"Hello from foo"
CTypeError: foo is not a function
Dundefined
Attempts:
2 left
πŸ’‘ Hint
Function declarations are hoisted completely, so they can be called before their definition.
❓ Predict Output
intermediate
2: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';
};
ATypeError: bar is not a function
B"Hello from bar"
CReferenceError: bar is not defined
Dundefined
Attempts:
2 left
πŸ’‘ Hint
Variable declarations are hoisted but not their assignments.
❓ Predict Output
advanced
2: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);
A
5
number
B
function baz() { return 10; }
function
C
function baz() { return 10; }
number
D
undefined
number
Attempts:
2 left
πŸ’‘ Hint
Function declarations are hoisted before variable declarations, but variable assignments override the function.
❓ Predict Output
advanced
2: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';
};
AReferenceError: Cannot access 'qux' before initialization
BTypeError: qux is not a function
C"Hello from qux"
Dundefined
Attempts:
2 left
πŸ’‘ Hint
Variables declared with let are not hoisted like var and cannot be accessed before initialization.
🧠 Conceptual
expert
2: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.
AFunction expressions are hoisted with their full definitions, but function declarations are hoisted only as variable declarations.
BFunction declarations and function expressions are both hoisted with their full definitions, so they can be called anywhere in the code.
CNeither function declarations nor function expressions are hoisted; they must be defined before use.
DFunction declarations are hoisted with their full definitions, but function expressions assigned to variables are hoisted only as variable declarations without their assignments.
Attempts:
2 left
πŸ’‘ Hint
Think about how JavaScript treats function declarations differently from variables assigned functions.