Bird
Raised Fist0
Javascriptprogramming~5 mins

Variable hoisting behavior in Javascript

Choose your learning style10 modes available
Introduction

Variable hoisting means JavaScript moves variable declarations to the top before running code. This helps avoid errors when variables are used before they are declared.

When you want to understand why a variable can be used before its declaration without error.
When debugging unexpected undefined values in your code.
When learning how JavaScript handles variable declarations inside functions or blocks.
When deciding between var, let, and const for declaring variables.
When reading or maintaining older JavaScript code that uses var.
Syntax
Javascript
var variableName;
let variableName;
const variableName = value;

var declarations are hoisted and initialized with undefined.

let and const declarations are hoisted but not initialized, causing a 'temporal dead zone' until their declaration line.

Examples
This prints undefined because var x is hoisted and initialized as undefined before assignment.
Javascript
console.log(x);
var x = 5;
This causes a ReferenceError because let y is hoisted but not initialized, so it cannot be accessed before declaration.
Javascript
console.log(y);
let y = 10;
Inside the function, var a is hoisted, so console.log(a) prints undefined.
Javascript
function test() {
  console.log(a);
  var a = 3;
}
test();
Sample Program

This program shows how var and let behave differently with hoisting. The var variable prints undefined before assignment. The let variable causes an error if accessed before declaration.

Javascript
console.log(message);
var message = 'Hello!';

try {
  console.log(greeting);
} catch (e) {
  console.log('Error:', e.message);
}

let greeting = 'Hi!';
OutputSuccess
Important Notes

Always declare variables at the top of their scope to avoid confusion.

Prefer let and const over var to prevent bugs related to hoisting.

Functions are also hoisted, but their behavior differs from variables.

Summary

JavaScript moves variable declarations to the top before running code.

var variables are hoisted and initialized as undefined.

let and const are hoisted but not initialized, causing errors if accessed early.

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