What if your code is running differently than you wrote it because of hidden moves JavaScript makes?
Why Variable hoisting behavior in Javascript? - Purpose & Use Cases
Imagine you write JavaScript code where you try to use a variable before you declare it, like grabbing a book from a shelf that isn't there yet.
Without understanding hoisting, your code might give unexpected results or errors because JavaScript moves variable declarations to the top behind the scenes, confusing beginners and causing bugs.
Knowing variable hoisting helps you predict how JavaScript treats your variables, so you can write clearer, bug-free code by understanding when variables exist and when they don't.
console.log(x);
var x = 5;var x = 5;
console.log(x);Understanding hoisting lets you avoid tricky bugs and write code that works exactly as you expect, even when variables appear before their declarations.
When debugging a program that crashes because a variable is used too early, knowing hoisting helps you quickly find and fix the problem.
Variables are treated as if declared at the top of their scope.
Using variables before declaration can cause unexpected results.
Understanding hoisting helps write clearer, more reliable code.
Practice
var variable declaration in JavaScript before code execution?Solution
Step 1: Understand var hoisting behavior
JavaScript movesvardeclarations to the top of their scope and initializes them asundefined.Step 2: Check initialization timing
The variable is not assigned its value until the code runs the assignment line, so before that it isundefined.Final Answer:
It is moved to the top and initialized as undefined. -> Option CQuick Check:
varhoisted = undefined [OK]
- Thinking var is not hoisted
- Assuming var is initialized with assigned value immediately
- Confusing var with let or const hoisting
Solution
Step 1: Identify hoisting behavior of let
letdeclarations are hoisted but not initialized, so accessing them before declaration causes a ReferenceError.Step 2: Check other options
varis hoisted and initialized as undefined, so no ReferenceError;constmust be initialized at declaration, soconst x;is a syntax error; function declarations are hoisted differently.Final Answer:
let x = 5;-> Option BQuick Check:
lethoisted but uninitialized causes ReferenceError [OK]
- Using const without initialization
- Confusing var and let hoisting
- Thinking function declarations behave like let
console.log(a); var a = 10; console.log(a);
Solution
Step 1: Analyze var hoisting for variable a
The declarationvar ais hoisted and initialized asundefinedbefore any code runs.Step 2: Trace console.log outputs
The firstconsole.log(a)printsundefinedbecause assignment hasn't happened yet. Aftera = 10, the secondconsole.log(a)prints10.Final Answer:
undefined 10 -> Option AQuick Check:
var hoisted as undefined, then assigned [OK]
- Expecting ReferenceError on first log
- Thinking var is assigned before declaration
- Confusing let/const behavior with var
console.log(b); let b = 20;
Solution
Step 1: Identify cause of ReferenceError
letvariables are hoisted but not initialized, so accessingbbefore declaration causes ReferenceError.Step 2: Fix by reordering code
Movingconsole.log(b)afterlet b = 20;ensuresbis declared before use, avoiding the error.Final Answer:
Moveconsole.log(b)after the declaration. -> Option DQuick Check:
Access let after declaration to avoid error [OK]
- Changing let to var without understanding scope
- Trying to initialize before declaration (syntax error)
- Assuming wrapping in function fixes hoisting
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?
Solution
Step 1: Analyze var x hoisting and scope
var xis hoisted to the function or global scope and initialized asundefined. So, firstconsole.log(x)printsundefined. After assignment inside the block,xbecomes 5.Step 2: Analyze let y block scope and access
let y = 10is block scoped. Inside the block,console.log(y)prints 10. Outside the block,console.log(y)causes ReferenceError becauseyis not defined there.Step 3: Trace remaining console.logs
After the block,console.log(x)prints 5. The lastconsole.log(y)throws ReferenceError.Final Answer:
undefined 10 5 ReferenceError -> Option AQuick Check:
var hoisted globally, let block scoped [OK]
- Assuming let is accessible outside block
- Expecting var to be block scoped
- Confusing initialization timing of var and let
Ready to prove it?
Pass the test and this pattern moves unknown
