Bird
Raised Fist0
Javascriptprogramming~20 mins

Variable hoisting behavior 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
Output of var hoisting in function scope
What is the output of this JavaScript code?
Javascript
function test() {
  console.log(x);
  var x = 5;
  console.log(x);
}
test();
Aundefined\nundefined
B5\n5
CReferenceError: x is not defined
Dundefined\n5
Attempts:
2 left
πŸ’‘ Hint
Remember that var declarations are hoisted but not their assignments.
❓ Predict Output
intermediate
2:00remaining
Output of let hoisting in block scope
What happens when this code runs?
Javascript
console.log(a);
let a = 10;
A10
BReferenceError: Cannot access 'a' before initialization
Cundefined
Dnull
Attempts:
2 left
πŸ’‘ Hint
Variables declared with let are hoisted but not initialized immediately.
❓ Predict Output
advanced
2:00remaining
Output of var and function hoisting combined
What is the output of this code snippet?
Javascript
console.log(foo);
var foo = 1;
function foo() {}
console.log(foo);
A[Function: foo]\n1
B1\n1
Cundefined\n1
DReferenceError: foo is not defined
Attempts:
2 left
πŸ’‘ Hint
Function declarations are hoisted before var declarations.
❓ Predict Output
advanced
2:00remaining
Output of const hoisting and temporal dead zone
What will this code output or error?
Javascript
console.log(bar);
const bar = 3;
ATypeError: Assignment to constant variable.
Bundefined
CReferenceError: Cannot access 'bar' before initialization
D3
Attempts:
2 left
πŸ’‘ Hint
Const variables behave like let in hoisting and temporal dead zone.
🧠 Conceptual
expert
2:00remaining
Why does this code produce 'undefined' instead of error?
Consider this code:
console.log(x);
var x = 7;

Why does it print undefined instead of throwing an error?
ABecause var declarations are hoisted and initialized with undefined before code runs
BBecause the assignment happens before the console.log
CBecause console.log ignores errors for var variables
DBecause JavaScript treats undeclared variables as undefined automatically
Attempts:
2 left
πŸ’‘ Hint
Think about what hoisting means for var variables.

Practice

(1/5)
1. What happens to a var variable declaration in JavaScript before code execution?
easy
A. It is not moved and must be declared before use.
B. It causes a syntax error if used before declaration.
C. It is moved to the top and initialized as undefined.
D. It is moved to the top and initialized with its assigned value.

Solution

  1. Step 1: Understand var hoisting behavior

    JavaScript moves var declarations to the top of their scope and initializes them as undefined.
  2. Step 2: Check initialization timing

    The variable is not assigned its value until the code runs the assignment line, so before that it is undefined.
  3. Final Answer:

    It is moved to the top and initialized as undefined. -> Option C
  4. Quick Check:

    var hoisted = undefined [OK]
Hint: Remember: var is hoisted and set to undefined first [OK]
Common Mistakes:
  • Thinking var is not hoisted
  • Assuming var is initialized with assigned value immediately
  • Confusing var with let or const hoisting
2. Which of the following is the correct way to declare a variable that is hoisted but causes a ReferenceError if accessed before declaration?
easy
A. var x = 5;
B. let x = 5;
C. function x() {}
D. const x;

Solution

  1. Step 1: Identify hoisting behavior of let

    let declarations are hoisted but not initialized, so accessing them before declaration causes a ReferenceError.
  2. Step 2: Check other options

    var is hoisted and initialized as undefined, so no ReferenceError; const must be initialized at declaration, so const x; is a syntax error; function declarations are hoisted differently.
  3. Final Answer:

    let x = 5; -> Option B
  4. Quick Check:

    let hoisted but uninitialized causes ReferenceError [OK]
Hint: let and const cause errors if used before declaration [OK]
Common Mistakes:
  • Using const without initialization
  • Confusing var and let hoisting
  • Thinking function declarations behave like let
3. What is the output of the following code?
console.log(a);
var a = 10;
console.log(a);
medium
A. undefined 10
B. 10 10
C. ReferenceError undefined
D. undefined undefined

Solution

  1. Step 1: Analyze var hoisting for variable a

    The declaration var a is hoisted and initialized as undefined before any code runs.
  2. Step 2: Trace console.log outputs

    The first console.log(a) prints undefined because assignment hasn't happened yet. After a = 10, the second console.log(a) prints 10.
  3. Final Answer:

    undefined 10 -> Option A
  4. Quick Check:

    var hoisted as undefined, then assigned [OK]
Hint: var logs undefined before assignment, then assigned value [OK]
Common Mistakes:
  • Expecting ReferenceError on first log
  • Thinking var is assigned before declaration
  • Confusing let/const behavior with var
4. Find the error in the following code and choose the correct fix:
console.log(b);
let b = 20;
medium
A. Change let to var to avoid ReferenceError.
B. Wrap the code in a function to fix hoisting.
C. Initialize b with a value before logging.
D. Move console.log(b) after the declaration.

Solution

  1. Step 1: Identify cause of ReferenceError

    let variables are hoisted but not initialized, so accessing b before declaration causes ReferenceError.
  2. Step 2: Fix by reordering code

    Moving console.log(b) after let b = 20; ensures b is declared before use, avoiding the error.
  3. Final Answer:

    Move console.log(b) after the declaration. -> Option D
  4. Quick Check:

    Access let after declaration to avoid error [OK]
Hint: Use variables only after let/const declaration [OK]
Common Mistakes:
  • Changing let to var without understanding scope
  • Trying to initialize before declaration (syntax error)
  • Assuming wrapping in function fixes hoisting
5. Consider this code:
console.log(x);
if (true) {
  var x = 5;
  let y = 10;
  console.log(y);
}
console.log(x);
console.log(y);

What will be the output and why?
hard
A. undefined 10 5 ReferenceError
B. ReferenceError 10 5 ReferenceError
C. undefined 10 5 10
D. undefined ReferenceError 5 ReferenceError

Solution

  1. Step 1: Analyze var x hoisting and scope

    var x is hoisted to the function or global scope and initialized as undefined. So, first console.log(x) prints undefined. After assignment inside the block, x becomes 5.
  2. Step 2: Analyze let y block scope and access

    let y = 10 is block scoped. Inside the block, console.log(y) prints 10. Outside the block, console.log(y) causes ReferenceError because y is not defined there.
  3. Step 3: Trace remaining console.logs

    After the block, console.log(x) prints 5. The last console.log(y) throws ReferenceError.
  4. Final Answer:

    undefined 10 5 ReferenceError -> Option A
  5. Quick Check:

    var hoisted globally, let block scoped [OK]
Hint: var is function/global scoped; let is block scoped [OK]
Common Mistakes:
  • Assuming let is accessible outside block
  • Expecting var to be block scoped
  • Confusing initialization timing of var and let
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