Bird
Raised Fist0
Javascriptprogramming~20 mins

What hoisting is in Javascript - Practice Problems & Coding Challenges

Choose your learning style10 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 involving hoisting?
Consider this JavaScript code snippet. What will it print to the console?
Javascript
console.log(a);
var a = 5;
Aundefined
B5
CReferenceError
Dnull
Attempts:
2 left
πŸ’‘ Hint
Think about how variable declarations are handled before code runs.
❓ Predict Output
intermediate
2: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();
Ahello
BReferenceError
Cundefined
Dnull
Attempts:
2 left
πŸ’‘ Hint
Remember that variable declarations are hoisted but assignments are not.
❓ Predict Output
advanced
2: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;
AReferenceError
Bundefined
Cnull
D10
Attempts:
2 left
πŸ’‘ Hint
Variables declared with let are not hoisted like var.
❓ Predict Output
advanced
2:00remaining
What will this code output with function hoisting?
What does this code print when run?
Javascript
foo();
function foo() {
  console.log('Hi');
}
AReferenceError
Bundefined
CTypeError
DHi
Attempts:
2 left
πŸ’‘ Hint
Function declarations are hoisted completely.
❓ Predict Output
expert
2: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() {}
Astring
Bfunction
Cundefined
DReferenceError
Attempts:
2 left
πŸ’‘ Hint
Function declarations are hoisted before variable declarations.

Practice

(1/5)
1. What does hoisting mean in JavaScript?
easy
A. JavaScript converts all variables to global scope automatically.
B. JavaScript moves variable and function declarations to the top before running code.
C. JavaScript delays execution of code until user interaction.
D. JavaScript runs code only after the entire file is loaded.

Solution

  1. Step 1: Understand hoisting concept

    Hoisting means JavaScript moves declarations of variables and functions to the top of their scope before running the code.
  2. Step 2: Compare options

    Only JavaScript moves variable and function declarations to the top before running code. correctly describes this behavior. Other options describe unrelated behaviors.
  3. Final Answer:

    JavaScript moves variable and function declarations to the top before running code. -> Option B
  4. Quick Check:

    Hoisting = Moving declarations up [OK]
Hint: Hoisting means declarations move up before code runs [OK]
Common Mistakes:
  • Thinking hoisting moves variable assignments, not just declarations
  • Confusing hoisting with asynchronous code execution
  • Believing hoisting applies to all variable types equally
2. Which of the following variable declarations is hoisted and initialized as undefined?
easy
A. function foo() {}
B. const y = 10;
C. let x = 5;
D. var z = 15;

Solution

  1. Step 1: Identify hoisting behavior for variable types

    Variables declared with var are hoisted and initialized as undefined. let and const are hoisted but not initialized, causing a temporal dead zone.
  2. Step 2: Match options to behavior

    var z = 15; uses var, so it is hoisted and initialized as undefined. Others do not behave this way.
  3. Final Answer:

    var z = 15; -> Option D
  4. Quick Check:

    var hoisted as undefined [OK]
Hint: Only var variables are hoisted and set to undefined [OK]
Common Mistakes:
  • Assuming let and const behave like var with hoisting
  • Confusing function declarations with variable declarations
  • Thinking function expressions are hoisted like functions
3. What will be the output of this code?
console.log(myVar);
var myVar = 10;
medium
A. undefined
B. 10
C. ReferenceError
D. null

Solution

  1. Step 1: Understand var hoisting with initialization

    The declaration var myVar is hoisted to the top, but the assignment = 10 stays in place.
  2. Step 2: Evaluate console.log output

    At the time of console.log(myVar), myVar exists but is undefined because assignment hasn't happened yet.
  3. Final Answer:

    undefined -> Option A
  4. Quick Check:

    var declaration hoisted, value undefined [OK]
Hint: var declaration hoisted, value assigned later [OK]
Common Mistakes:
  • Expecting the value 10 to print immediately
  • Confusing ReferenceError with undefined for var
  • Thinking let or const behave the same way
4. Find the error in this code related to hoisting:
console.log(foo());
var foo = function() { return 'Hello'; };
medium
A. No error, outputs 'Hello'
B. SyntaxError
C. TypeError: foo is not a function
D. ReferenceError: foo is not defined

Solution

  1. Step 1: Understand hoisting of function expressions

    Variable foo is hoisted as undefined, but the function assignment is not hoisted.
  2. Step 2: Analyze calling foo before assignment

    Calling foo() before assignment tries to call undefined as a function, causing a TypeError.
  3. Final Answer:

    TypeError: foo is not a function -> Option C
  4. Quick Check:

    Function expressions not hoisted as functions [OK]
Hint: Function expressions hoisted as undefined, not callable [OK]
Common Mistakes:
  • Thinking function expressions are fully hoisted
  • Expecting no error because of hoisting
  • Confusing ReferenceError with TypeError
5. Consider this code:
console.log(greet());

function greet() {
  return 'Hi!';
}

var greet = function() {
  return 'Hello!';
};

console.log(greet());

What will be the output?
hard
A. 'Hi!' then ReferenceError
B. 'Hello!' then 'Hello!'
C. ReferenceError then 'Hello!'
D. 'Hi!' then 'Hello!'

Solution

  1. Step 1: Analyze hoisting of function declaration

    The function declaration function greet() is fully hoisted, so the first greet() call returns 'Hi!'.
  2. Step 2: Analyze var assignment after first call

    The var greet declaration is hoisted but initialized as undefined. The assignment to a function expression happens after the first call, so the second greet() call uses the new function returning 'Hello!'.
  3. Final Answer:

    'Hi!' then ReferenceError -> Option A
  4. Quick Check:

    Function declaration hoisted, var assignment overrides later [OK]
Hint: Function declarations hoisted fully; var assignments override later [OK]
Common Mistakes:
  • Thinking var assignment overrides before first call
  • Expecting ReferenceError due to var redeclaration
  • Confusing function declaration and expression hoisting
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard