Skip to content
-
Subscribe to our newsletter & never miss our best posts. Subscribe Now!
Developer Hint

Your Ultimate Guide to Web Development.

Developer Hint

Your Ultimate Guide to Web Development.

  • Home
  • Web Development
  • Tech Explained
  • Developer Tools
  • Contact Us
  • Home
  • Web Development
  • Tech Explained
  • Developer Tools
  • Contact Us
Close

Search

Subscribe
Developer Hint

Your Ultimate Guide to Web Development.

Developer Hint

Your Ultimate Guide to Web Development.

  • Home
  • Web Development
  • Tech Explained
  • Developer Tools
  • Contact Us
  • Home
  • Web Development
  • Tech Explained
  • Developer Tools
  • Contact Us
Close

Search

Subscribe
Home/Web Development/JavaScript Hoisting Explained: Avoid Common Mistakes
What Is Javascript Hoisting
Web Development

JavaScript Hoisting Explained: Avoid Common Mistakes

blank
By Developer Hint
January 22, 2026 2 Min Read
0

Introduction

Have you ever used a variable in JavaScript before declaring it and wondered why the code didn’t break?

That behavior is caused by JavaScript hoisting.

Hoisting is one of the most confusing concepts for beginners, but once you understand how it works, JavaScript starts to make much more sense.

What Is JavaScript Hoisting?

Hoisting is JavaScript’s default behavior of moving declarations to the top of their scope before code execution.

Important:

  • Declarations are hoisted
  • Assignments are not hoisted

Think of hoisting as JavaScript saying:

“I know this variable or function exists before running the code.”

Hoisting with var

Example

console.log(name);
var name = "Ali";

What Happens

JavaScript treats the code like this:

var name;
console.log(name);
name = "Ali";

Output:

undefined

✔ No error, but the value is undefined.

Hoisting with let and const

Variables declared with let and const are hoisted, but they are not accessible before declaration.

Example

console.log(age);
let age = 20;

❌ This causes an error:

ReferenceError: Cannot access 'age' before initialization

This happens because of the Temporal Dead Zone (TDZ).

Temporal Dead Zone (TDZ)

The TDZ is the time between:

  • When the variable is hoisted
  • And when it is declared

During this time, accessing the variable causes an error.

✔ This makes code safer and avoids bugs.

Function Hoisting

Function Declarations

Function declarations are fully hoisted.

sayHello();
function sayHello() {
console.log("Hello!");
}

✔ Works perfectly.

Function Expressions

Function expressions are not hoisted like declarations.

sayHi();
var sayHi = function () {
console.log("Hi!");
};

❌ Error:

TypeError: sayHi is not a function

Hoisting Summary Table

TypeHoistedUsable Before Declaration
varYesYes (undefined)
letYesNo
constYesNo
Function declarationYesYes
Function expressionPartialNo

Common Beginner Mistakes

  • Thinking hoisting moves code physically
  • Using var unintentionally
  • Calling function expressions too early
  • Ignoring TDZ errors

Best Practices

  • Prefer let and const over var
  • Declare variables at the top of their scope
  • Define functions before using them
  • Write clear, predictable code

Conclusion

JavaScript hoisting explains why variables and functions sometimes work before they are declared. Understanding how var, let, const, and functions behave with hoisting will help you avoid bugs and write cleaner JavaScript.

At Developer Hint we simplify JavaScript concepts so beginners can understand how things work behind the scenes and build confidence step by step.


Discover more from Developer Hint

Subscribe to get the latest posts sent to your email.

Content Disclosure
This content was created with the assistance of AI tools and thoroughly reviewed, fact-checked, and refined by a human editor to ensure accuracy, clarity, and usefulness for readers.
Advertisements
banner

Tags:

JavaScriptVS CodeWeb Development
blank
Author

Developer Hint

Follow Me
Other Articles
Ssl
Previous

Understanding SSL: Why It’s Essential for Your Website

Var Vs Let Vs Const In Javascript
Next

JavaScript Variable Declarations: Understanding var, let, and const

No Comment! Be the first one.

    Leave a ReplyCancel reply

    Random Posts

    • 7 Mistakes That Keep Web Developers Stuck at Beginner Level7 Mistakes That Keep Web Developers Stuck at Beginner Level
    • What Is the Internet? Definition, History, and How It Works (Complete Guide)What Is the Internet? Definition, History, and How It Works (Complete Guide)
    • Responsive Web Design with Bootstrap’s Grid SystemResponsive Web Design with Bootstrap’s Grid System
    • HTML vs HTML5: What’s the Real Difference?HTML vs HTML5: What’s the Real Difference?
    • Client-Side vs Server-Side Rendering: Whatโ€™s the Difference?Client-Side vs Server-Side Rendering: Whatโ€™s the Difference?

    Popular

    Random Posts

    • Client-Side vs Server-Side Rendering: Whatโ€™s the Difference?Client-Side vs Server-Side Rendering: Whatโ€™s the Difference?
    • WCAG Guidelines Explained Simply for BeginnersWCAG Guidelines Explained Simply for Beginners
    • Frontend vs Backend: Which One Should You Learn First in 2026?Frontend vs Backend: Which One Should You Learn First in 2026?
    • Top 10 VS Code Extensions Every Developer Should Use in 2026Top 10 VS Code Extensions Every Developer Should Use in 2026
    • Core Web Vitals Explained for Beginners: A Developer’s Guide to Website PerformanceCore Web Vitals Explained for Beginners: A Developer’s Guide to Website Performance

    Legal pages

    • About Us
    • Privacy Policy
    • Terms and Conditions
    • Disclaimer

    Trending

    Copyright 2026 โ€” Developer Hint. All rights reserved.

    Necessary cookies enable essential site features like secure log-ins and consent preference adjustments. They do not store personal data.
    None
    Functional cookies support features like content sharing on social media, collecting feedback, and enabling third-party tools.
    None
    Analytical cookies track visitor interactions, providing insights on metrics like visitor count, bounce rate, and traffic sources.
    None
    Advertisement cookies deliver personalized ads based on your previous visits and analyze the effectiveness of ad campaigns.
    None
    Unclassified cookies are cookies that we are in the process of classifying, together with the providers of individual cookies.
    None