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.
Variable hoisting behavior in 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.
undefined because var x is hoisted and initialized as undefined before assignment.console.log(x); var x = 5;
let y is hoisted but not initialized, so it cannot be accessed before declaration.console.log(y); let y = 10;
var a is hoisted, so console.log(a) prints undefined.function test() { console.log(a); var a = 3; } test();
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.
console.log(message); var message = 'Hello!'; try { console.log(greeting); } catch (e) { console.log('Error:', e.message); } let greeting = 'Hi!';
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.
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
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
