DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
← From Beginner to Expert in Fundamentals of Frontend
  • Scope & Closures
  • Asynchronous JavaScript
  • DOM Manipulation
  • ES6+ to 2026
  • Execution Context & Call Stack
  • Hoisting: var, let, const & Functions
  • Data Types & Type Coercion
  • Prototypes & the this Keyword
  • The Event Loop Deep Dive
  • Higher-Order Functions, Currying & Functional Patterns
  • Callbacks, Promises & Async/Await
  • Event Delegation & DOM Optimization
  • Destructuring, Spread/Rest & Modern Syntax
  • ES Modules & the Fetch API
  • Memory Management & Garbage Collection
  • Debouncing, Throttling & Performance Patterns
  • Design Patterns in JavaScript
  • Proxies, Reflect & Symbols
  • JavaScript Optimizations & Best Practices
  • JavaScript Interview Questions (30 MCQs)
← From Beginner to Expert in Fundamentals of Frontend

Hoisting: var, let, const & Functions

18 min read
HoistingTDZvar vs let vs constFunction DeclarationsBlock ScopeFunction Scope

What is Hoisting?

Hoisting is JavaScript's behavior of moving declarations (not assignments) to the top of their scope during the Creation Phase of the execution context.

How Different Declarations Are Hoisted

DeclarationHoisted?Initial ValueScope
var✅ YesundefinedFunction
let✅ Yes (but TDZ)UninitializedBlock
const✅ Yes (but TDZ)UninitializedBlock
function✅ Yes (fully)Entire functionFunction
class✅ Yes (but TDZ)UninitializedBlock

The Temporal Dead Zone (TDZ)

The TDZ is the period between entering a scope and the variable's declaration being executed. Accessing a let or const variable in its TDZ throws a ReferenceError.

// TDZ for `name` starts here
console.log(name); // ❌ ReferenceError: Cannot access 'name' before initialization
let name = "Alice"; // TDZ ends here

Function Hoisting vs Expression Hoisting

// ✅ Function Declaration — fully hoisted
greet(); // Works! "Hello"
function greet() { console.log("Hello"); }

// ❌ Function Expression — only var is hoisted (as undefined)
sayHi(); // TypeError: sayHi is not a function
var sayHi = function() { console.log("Hi"); };

// ❌ Arrow Function with const — TDZ
sayBye(); // ReferenceError
const sayBye = () => console.log("Bye");

Why var Causes Bugs

for (var i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 100);
}
// Output: 3, 3, 3 (var is function-scoped, shares one `i`)

for (let j = 0; j < 3; j++) {
  setTimeout(() => console.log(j), 100);
}
// Output: 0, 1, 2 (let is block-scoped, each iteration gets its own `j`)

Best Practices

  • Use const by default
  • Use let only when you need reassignment
  • Never use var in modern JavaScript
  • Declare variables at the top of their scope for clarity

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.