DEV Community

Cover image for What is Hoisting in JavaScript? | Explained with Examples & Visuals
WebTechnology Tutorials
WebTechnology Tutorials

Posted on • Edited on • Originally published at webcodingwithankur.blogspot.com

What is Hoisting in JavaScript? | Explained with Examples & Visuals

What is Hoisting in JavaScript


๐Ÿš€ Introduction
Have you ever encountered an error like undefined is not a function or wondered why a variable seems to exist before it's declared?
Welcome to the quirky world of hoisting in JavaScript โ€” a concept that surprises most developers when they first see it in action.

In this blog post, you'll learn:

  • What is hoisting in JavaScript?
  • How variables and functions are hoisted
  • The difference between var, let, and const
  • Real examples and a mental model to remember hoisting

๐Ÿ” What is Hoisting?
Hoisting is JavaScriptโ€™s default behavior of moving declarations to the top of the current scope (function or global).

This means you can use variables and functions before you declare them โ€” but thereโ€™s a catch!


๐Ÿ“Œ Example 1: Variable Hoisting with var

console.log(name); // undefined
var name = "Ankur";

Enter fullscreen mode Exit fullscreen mode

Explanation:
The variable name is hoisted, but only the declaration (not the value) is moved to the top.Itโ€™s as if you wrote:

var name;
console.log(name); // undefined
name = "Ankur";

Enter fullscreen mode Exit fullscreen mode

๐Ÿšซ What about let and const?

console.log(age); // โŒ ReferenceError
let age = 25;

Enter fullscreen mode Exit fullscreen mode

Variables declared with letand const are not initialized during hoisting.
They remain in a Temporal Dead Zone (TDZ) until the line of declaration is reached.


๐Ÿ“Œ Example 2: Function Hoisting

greet();

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

Enter fullscreen mode Exit fullscreen mode

โœ… This works!
Function declarations are hoisted with their definitions.


โš ๏ธ Function Expressions are NOT Hoisted

sayHi(); // โŒ TypeError

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

Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Visual Model of Hoisting

Visual Model of Hoisting


๐Ÿ“ Summary

Concept Hoisted? Accessible before declaration?
var โœ… Yes โš ๏ธ Yes, but value is undefined
let / const โœ… Yes โŒ No, in TDZ
Function Declaration โœ… Yes โœ… Yes
Function Expression โŒ No โŒ No

๐Ÿ’ฌ Final Thoughts
Understanding hoisting helps prevent bugs and write cleaner JavaScript code.
Next time you write a function or a variable, remember this internal "lift" that JavaScript performs.


๐Ÿ”— Read Full Blog with Code and Visuals
๐Ÿ‘‰ https://webcodingwithankur.blogspot.com/2025/06/what-is-hoisting-in-javascript.html

Top comments (2)

Collapse
 
alexmustiere profile image
Alex Mustiere

A function expression using var is hoisted.
When you write:

sayHi();
var sayHi = function () {
  console.log("Hi!");
};
Enter fullscreen mode Exit fullscreen mode

Itโ€™s as if you wrote:

var sayHi; // sayHi is undefined at this point
sayHi(); // โŒ TypeError (you can't invoke on undefined)
sayHi = function () {
  console.log("Hi!");
};
Enter fullscreen mode Exit fullscreen mode

And if you use let or const you get a ReferenceError.
And the behavior is the same if you use "classic" or "arrow" functions.

Collapse
 
webtechnology_tutorials_f profile image
WebTechnology Tutorials

so , you mean that i have written wrong something in this post.