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.
Common hoisting pitfalls in 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.
console.log(a); // undefined var a = 3;
foo(); function foo() { console.log('Hi'); }
console.log(b); // ReferenceError let b = 4;
bar(); var bar = function() { console.log('Hey'); };
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.
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'); };
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.
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
var variable before its declaration in JavaScript?Solution
Step 1: Understand var hoisting behavior
Variables declared withvarare hoisted to the top and initialized withundefined.Step 2: Access before declaration
Accessing the variable before its declaration returnsundefinedinstead of an error.Final Answer:
It returns undefined because var is hoisted and initialized with undefined. -> Option AQuick Check:
var hoisting = undefined [OK]
- Thinking accessing var before declaration throws error
- Confusing var with let or const hoisting
- Assuming var is not hoisted at all
Solution
Step 1: Recall let hoisting
letis hoisted but not initialized, causing a temporal dead zone.Step 2: Identify which causes ReferenceError before declaration
Accessingletbefore declaration throws ReferenceError. Here, let x = 10; useslet.Final Answer:
let x = 10; -> Option DQuick Check:
let before declaration = ReferenceError [OK]
- Thinking var causes ReferenceError before declaration
- Confusing function declarations with variables
- Assuming const behaves like var
console.log(foo());
function foo() {
return 'Hello';
}
var foo = function() {
return 'World';
};Solution
Step 1: Understand function declaration hoisting
Function declarations are fully hoisted, sofoois initially the function returning 'Hello'.Step 2: Analyze var function expression assignment
Thevar foodeclaration 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.Final Answer:
'Hello' -> Option AQuick Check:
Function declarations hoisted fully = 'Hello' [OK]
- Assuming var function expression overwrites before call
- Confusing function declaration and expression hoisting
- Expecting TypeError or ReferenceError
console.log(a); let a = 5;
Solution
Step 1: Recognize temporal dead zone with let
Variables declared withletare hoisted but not initialized, causing ReferenceError if accessed before declaration.Step 2: Fix by moving declaration before usage
Movelet a = 5;beforeconsole.log(a);to avoid ReferenceError.Final Answer:
ReferenceError because let is accessed before declaration -> Option CQuick Check:
let before declaration = ReferenceError [OK]
- Thinking let behaves like var
- Ignoring temporal dead zone
- Assuming missing semicolon causes error
console.log(typeof bar); var bar = 'test'; console.log(typeof baz); let baz = 'hello';
What will be the output and why?
Solution
Step 1: Analyze typeof bar with var
var baris hoisted and initialized withundefined, sotypeof barreturns 'undefined'.Step 2: Analyze typeof baz with let
let bazis hoisted but not initialized, accessing it before declaration causes ReferenceError, even with typeof.Final Answer:
'undefined' followed by ReferenceError -> Option BQuick Check:
var hoisted initialized, let TDZ causes ReferenceError [OK]
- Assuming typeof let before declaration returns 'undefined'
- Confusing var and let hoisting behavior
- Expecting no error from typeof on let
Ready to prove it?
Pass the test and this pattern moves unknown
