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

Simple Coding Tips For Developers.

Developer Hint

Simple Coding Tips For Developers.

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

Search

Trending Now:
5 Essential Tools Every Blogger Should Use Music Trends That Will Dominate This Year ChatGPT prompts – AI content & image creation trend Ghibli trend – viral anime-style visual trend
Subscribe
Developer Hint

Simple Coding Tips For Developers.

Developer Hint

Simple Coding Tips For Developers.

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

Search

Trending Now:
5 Essential Tools Every Blogger Should Use Music Trends That Will Dominate This Year ChatGPT prompts – AI content & image creation trend Ghibli trend – viral anime-style visual trend
Subscribe
Home/Web Development/JavaScript Hoisting Explained: Avoid Common Mistakes
What Is Javascript Hoisting
Web Development

JavaScript Hoisting Explained: Avoid Common Mistakes

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.

Share this:

  • Share on Facebook (Opens in new window) Facebook
  • Share on X (Opens in new window) X
  • Share on Telegram (Opens in new window) Telegram
  • Share on WhatsApp (Opens in new window) WhatsApp
  • Email a link to a friend (Opens in new window) Email
  • Print (Opens in new window) Print

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
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

    • Web Accessibility Testing Tools for Developers (Free & Paid)Web Accessibility Testing Tools for Developers (Free & Paid)
    • Essential VS Code Code Formatting TipsEssential VS Code Code Formatting Tips
    • How to Speed Up Your WordPress Website (Complete Performance Guide)How to Speed Up Your WordPress Website (Complete Performance Guide)
    • Understanding Web Hosting: A Complete Guide for BeginnersUnderstanding Web Hosting: A Complete Guide for Beginners
    • Full Stack Web Developer Roadmap 2026: Build Modern Web Applications ProfessionallyFull Stack Web Developer Roadmap 2026: Build Modern Web Applications Professionally

    Popular

    Random Posts

    • Web Accessibility Testing Tools for Developers (Free & Paid)Web Accessibility Testing Tools for Developers (Free & Paid)
    • How to Speed Up Your WordPress Website (Complete Performance Guide)How to Speed Up Your WordPress Website (Complete Performance Guide)
    • CSS Box Model Explained Simply (With Practical Examples)CSS Box Model Explained Simply (With Practical Examples)
    • JavaScript Variable Declarations: Understanding var, let, and constJavaScript Variable Declarations: Understanding var, let, and const
    • Best Free Tools for Web Developers in 2026Best Free Tools for Web Developers in 2026

    Legal pages

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

    Subscribe to Blog via Email

    Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    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