Bird
Raised Fist0
Javascriptprogramming~5 mins

Common hoisting pitfalls in Javascript

Choose your learning style10 modes available
Introduction

Hoisting is how JavaScript moves some declarations to the top before running code. Knowing common hoisting pitfalls helps avoid bugs where variables or functions seem to act strangely.

When you declare variables with var and wonder why they are undefined before assignment.
When you use functions before they are written in the code and want to understand why some work and some don't.
When debugging errors related to variables or functions being used before they are declared.
When learning how let and const differ from var in terms of hoisting.
When writing clean code to avoid unexpected behavior caused by hoisting.
Syntax
Javascript
// var declarations are hoisted
console.log(x); // undefined
var x = 5;

// function declarations are hoisted
foo(); // works
function foo() { console.log('hello'); }

// let and const are hoisted but not initialized
console.log(y); // ReferenceError
let y = 10;

Only declarations are hoisted, not initializations.

var variables are hoisted and initialized with undefined.

Examples
var a is hoisted and set to undefined before assignment, so logging it shows undefined.
Javascript
console.log(a); // undefined
var a = 3;
Function declarations are fully hoisted, so calling foo() before its code works fine.
Javascript
foo();
function foo() { console.log('Hi'); }
let variables are hoisted but not initialized, so accessing b before declaration causes an error.
Javascript
console.log(b); // ReferenceError
let b = 4;
Function expressions assigned to var are not hoisted as functions, only the var declaration is hoisted as undefined, so calling bar() before assignment causes a TypeError.
Javascript
bar();
var bar = function() { console.log('Hey'); };
Sample Program

This program shows different hoisting behaviors: var x is hoisted and initialized as undefined, so first log prints undefined. Accessing let y before declaration causes ReferenceError. Function foo() is hoisted fully so calling it before declaration works. Calling bar() before assignment causes TypeError because bar is undefined at that time.

Javascript
console.log(x); // undefined
var x = 1;

console.log(y); // ReferenceError
let y = 2;

foo(); // works
function foo() { console.log('foo called'); }

bar(); // TypeError
var bar = function() { console.log('bar called'); };
OutputSuccess
Important Notes

var declarations are hoisted and initialized with undefined, so you can access them before assignment but get undefined.

let and const are hoisted but not initialized, causing a 'temporal dead zone' error if accessed early.

Function declarations are hoisted completely, but function expressions are not hoisted as functions.

Summary

var declarations are hoisted and initialized with undefined.

let and const are hoisted but accessing them before declaration causes errors.

Function declarations are fully hoisted, function expressions are not.

Practice

(1/5)
1. What happens when you access a var variable before its declaration in JavaScript?
easy
A. It returns undefined because var is hoisted and initialized with undefined.
B. It throws a ReferenceError because the variable is not defined yet.
C. It returns the value assigned later in the code.
D. It causes a SyntaxError.

Solution

  1. Step 1: Understand var hoisting behavior

    Variables declared with var are hoisted to the top and initialized with undefined.
  2. Step 2: Access before declaration

    Accessing the variable before its declaration returns undefined instead of an error.
  3. Final Answer:

    It returns undefined because var is hoisted and initialized with undefined. -> Option A
  4. Quick Check:

    var hoisting = undefined [OK]
Hint: var is hoisted and set to undefined before code runs [OK]
Common Mistakes:
  • Thinking accessing var before declaration throws error
  • Confusing var with let or const hoisting
  • Assuming var is not hoisted at all
2. Which of the following is the correct way to declare a variable that will cause a ReferenceError if accessed before declaration?
easy
A. x = 10;
B. var x = 10;
C. function x() {}
D. let x = 10;

Solution

  1. Step 1: Recall let hoisting

    let is hoisted but not initialized, causing a temporal dead zone.
  2. Step 2: Identify which causes ReferenceError before declaration

    Accessing let before declaration throws ReferenceError. Here, let x = 10; uses let.
  3. Final Answer:

    let x = 10; -> Option D
  4. Quick Check:

    let before declaration = ReferenceError [OK]
Hint: let causes ReferenceError if accessed early [OK]
Common Mistakes:
  • Thinking var causes ReferenceError before declaration
  • Confusing function declarations with variables
  • Assuming const behaves like var
3. What is the output of the following code?
console.log(foo());

function foo() {
  return 'Hello';
}

var foo = function() {
  return 'World';
};
medium
A. 'Hello'
B. 'World'
C. TypeError
D. ReferenceError

Solution

  1. Step 1: Understand function declaration hoisting

    Function declarations are fully hoisted, so foo is initially the function returning 'Hello'.
  2. Step 2: Analyze var function expression assignment

    The var foo declaration is hoisted but the assignment to function expression happens after the call, so it does not overwrite the function declaration at the time of call.
  3. Final Answer:

    'Hello' -> Option A
  4. Quick Check:

    Function declarations hoisted fully = 'Hello' [OK]
Hint: Function declarations hoisted fully, var function expressions not [OK]
Common Mistakes:
  • Assuming var function expression overwrites before call
  • Confusing function declaration and expression hoisting
  • Expecting TypeError or ReferenceError
4. Identify the error in the following code and fix it:
console.log(a);
let a = 5;
medium
A. No error, outputs 5
B. SyntaxError due to missing semicolon
C. ReferenceError because let is accessed before declaration
D. TypeError because a is not a function

Solution

  1. Step 1: Recognize temporal dead zone with let

    Variables declared with let are hoisted but not initialized, causing ReferenceError if accessed before declaration.
  2. Step 2: Fix by moving declaration before usage

    Move let a = 5; before console.log(a); to avoid ReferenceError.
  3. Final Answer:

    ReferenceError because let is accessed before declaration -> Option C
  4. Quick Check:

    let before declaration = ReferenceError [OK]
Hint: Declare let variables before use to avoid ReferenceError [OK]
Common Mistakes:
  • Thinking let behaves like var
  • Ignoring temporal dead zone
  • Assuming missing semicolon causes error
5. Consider this code snippet:
console.log(typeof bar);
var bar = 'test';
console.log(typeof baz);
let baz = 'hello';

What will be the output and why?
hard
A. 'string' followed by 'string'
B. 'undefined' followed by ReferenceError
C. ReferenceError followed by 'undefined'
D. 'undefined' followed by 'undefined'

Solution

  1. Step 1: Analyze typeof bar with var

    var bar is hoisted and initialized with undefined, so typeof bar returns 'undefined'.
  2. Step 2: Analyze typeof baz with let

    let baz is hoisted but not initialized, accessing it before declaration causes ReferenceError, even with typeof.
  3. Final Answer:

    'undefined' followed by ReferenceError -> Option B
  4. Quick Check:

    var hoisted initialized, let TDZ causes ReferenceError [OK]
Hint: typeof var before declaration = 'undefined', let causes ReferenceError [OK]
Common Mistakes:
  • Assuming typeof let before declaration returns 'undefined'
  • Confusing var and let hoisting behavior
  • Expecting no error from typeof on 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