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.

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 Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Random Posts

    • Why VS Code is the Top Choice for DevelopersWhy VS Code is the Top Choice for Developers
    • ย 5 Essential Tools Every Blogger Should Useย 5 Essential Tools Every Blogger Should Use
    • CSS Selectors: Class and ID ExplainedCSS Selectors: Class and ID Explained
    • Full Stack Web Developer Roadmap 2026: Complete Guide from Beginner to AdvancedFull Stack Web Developer Roadmap 2026: Complete Guide from Beginner to Advanced
    • Client-Side vs Server-Side Rendering: Whatโ€™s the Difference?Client-Side vs Server-Side Rendering: Whatโ€™s the Difference?

    Popular

    Random Posts

    • CSS Selectors Explained with Examples (Detailed Guide)CSS Selectors Explained with Examples (Detailed Guide)
    • How to Build Consistency as a Web Developer (Even If Youโ€™re Busy)How to Build Consistency as a Web Developer (Even If Youโ€™re Busy)
    • CSS Variables Explained: A Practical Guide to Custom PropertiesCSS Variables Explained: A Practical Guide to Custom Properties
    • What Is a Computer? Definition, Types, and How It Works (Beginnerโ€™s Guide)What Is a Computer? Definition, Types, and How It Works (Beginnerโ€™s Guide)
    • How to Speed Up Your WordPress Website (Complete Performance Guide)How to Speed Up Your WordPress Website (Complete Performance Guide)

    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