Bird
Raised Fist0
Javascriptprogramming~20 mins

Hoisting with let and const in Javascript - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Hoisting Mastery
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 snippet?

Consider the following JavaScript code:

console.log(a);
let a = 5;

What will be printed to the console?

Javascript
console.log(a);
let a = 5;
Anull
BReferenceError
C5
Dundefined
Attempts:
2 left
πŸ’‘ Hint

Think about the temporal dead zone for let variables.

❓ Predict Output
intermediate
2:00remaining
What is the output of this code snippet?

Look at this code:

console.log(b);
const b = 10;

What will be the output?

Javascript
console.log(b);
const b = 10;
A10
BTypeError
Cundefined
DReferenceError
Attempts:
2 left
πŸ’‘ Hint

Remember how const variables behave before initialization.

❓ Predict Output
advanced
2:00remaining
What is the output of this code snippet?

Analyze this code:

var x = 1;
{
  console.log(x);
  let x = 2;
}

What will be printed?

Javascript
var x = 1;
{
  console.log(x);
  let x = 2;
}
AReferenceError
B2
C1
Dundefined
Attempts:
2 left
πŸ’‘ Hint

Consider the block scope and temporal dead zone of let.

❓ Predict Output
advanced
2:00remaining
What is the output of this code snippet?

Consider this code:

function test() {
  console.log(y);
  const y = 3;
}
test();

What will be printed when test() is called?

Javascript
function test() {
  console.log(y);
  const y = 3;
}
test();
AReferenceError
Bundefined
C3
DTypeError
Attempts:
2 left
πŸ’‘ Hint

Think about the temporal dead zone inside functions with const.

🧠 Conceptual
expert
2:00remaining
Which option correctly explains hoisting behavior of let and const?

Choose the statement that best describes how let and const variables behave during hoisting in JavaScript.

A<p><code>let</code> and <code>const</code> variables are hoisted and initialized with <code>undefined</code> before code execution.</p>
B<p><code>let</code> variables are hoisted and initialized, but <code>const</code> variables are not hoisted at all.</p>
C<p><code>let</code> and <code>const</code> variables are hoisted but not initialized, causing a temporal dead zone until their declaration is reached.</p>
D<p><code>let</code> and <code>const</code> variables behave exactly like <code>var</code> during hoisting.</p>
Attempts:
2 left
πŸ’‘ Hint

Recall the temporal dead zone concept for let and const.

Practice

(1/5)
1. What happens if you try to access a let variable before its declaration in JavaScript?
easy
A. It returns the variable's default value.
B. It throws a ReferenceError due to the temporal dead zone.
C. It returns null.
D. It returns undefined.

Solution

  1. Step 1: Understand hoisting behavior of let

    Variables declared with let are hoisted but not initialized immediately.
  2. Step 2: Access before declaration causes error

    Accessing such variables before declaration triggers a ReferenceError due to the temporal dead zone.
  3. Final Answer:

    It throws a ReferenceError due to the temporal dead zone. -> Option B
  4. Quick Check:

    Temporal Dead Zone = ReferenceError [OK]
Hint: Accessing before declaration with let/const causes ReferenceError [OK]
Common Mistakes:
  • Thinking it returns undefined like var
  • Assuming it returns null
  • Believing it has a default value
2. Which of the following is the correct syntax to declare a constant variable in JavaScript?
easy
A. const PI = 3.14;
B. let const PI = 3.14;
C. constant PI = 3.14;
D. var const PI = 3.14;

Solution

  1. Step 1: Recall correct const declaration syntax

    The correct way to declare a constant is using const followed by the variable name and assignment.
  2. Step 2: Identify invalid syntax options

    Options B, C, and D use invalid keywords or combinations not supported in JavaScript.
  3. Final Answer:

    const PI = 3.14; -> Option A
  4. Quick Check:

    const declaration syntax = const name = value [OK]
Hint: Use 'const name = value;' to declare constants [OK]
Common Mistakes:
  • Using 'constant' keyword instead of 'const'
  • Combining let or var with const
  • Missing assignment during declaration
3. What will be the output of the following code?
console.log(x);
let x = 10;
medium
A. 10
B. undefined
C. ReferenceError
D. null

Solution

  1. Step 1: Analyze variable declaration and usage

    The variable x is declared with let after the console.log statement.
  2. Step 2: Understand temporal dead zone effect

    Accessing x before its declaration causes a ReferenceError due to the temporal dead zone.
  3. Final Answer:

    ReferenceError -> Option C
  4. Quick Check:

    let before declaration = ReferenceError [OK]
Hint: Accessing let before declaration throws ReferenceError [OK]
Common Mistakes:
  • Assuming it prints undefined like var
  • Expecting the value 10 immediately
  • Thinking it prints null
4. Identify the error in the following code and fix it:
function test() {
  console.log(a);
  const a = 5;
}
test();
medium
A. No error, outputs 5
B. TypeError, fix by initializing a with null
C. SyntaxError, fix by changing const to var
D. ReferenceError, fix by moving const declaration before console.log

Solution

  1. Step 1: Identify the cause of error

    Accessing a before its const declaration causes a ReferenceError due to temporal dead zone.
  2. Step 2: Fix by reordering declaration

    Moving the const a = 5; line before console.log(a); fixes the error.
  3. Final Answer:

    ReferenceError, fix by moving const declaration before console.log -> Option D
  4. Quick Check:

    Declare const before use to avoid ReferenceError [OK]
Hint: Declare const before use to avoid ReferenceError [OK]
Common Mistakes:
  • Changing const to var without fixing order
  • Ignoring temporal dead zone
  • Assuming no error occurs
5. Consider the code below. What will be the output and why?
const a = 1;
{
  console.log(a);
  let a = 2;
}
hard
A. ReferenceError
B. 2
C. 1
D. undefined

Solution

  1. Step 1: Understand block scope and hoisting

    The block declares a new let a which is hoisted but uninitialized inside the block.
  2. Step 2: Accessing a before let a declaration

    Inside the block, console.log(a) tries to access the block-scoped a before its declaration, causing a ReferenceError due to temporal dead zone.
  3. Final Answer:

    ReferenceError -> Option A
  4. Quick Check:

    Block-scoped let shadows outer const causing ReferenceError [OK]
Hint: Block let shadows outer const; accessing before declaration errors [OK]
Common Mistakes:
  • Assuming it prints outer const value 1
  • Expecting it prints 2 before declaration
  • Thinking it prints undefined
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