Skip to main content
D
JavaScript
JavaScript Hoisting

JavaScript Hoisting

JavaScript hoisting moves variable and function declarations to the top.

Read Time
5 min read
Difficulty
Beginner
Last Updated
Jun 15, 2026
Version
v1.0.0

Introduction

JavaScript secretly moves all variable and function declarations to the very top of the script before running the code. Because of this strange behavior, developers can call a function on line 1 even if the function isn't written until line 50.

Example

Example
1/* this shows hoisting */
2x = 5;
3console.log(x);
4var x; // Declaration is hoisted

Key Points

  • Hoisting moves declarations, not assignments.
  • `var` declarations hoist and initialize as undefined.
  • `let` and `const` declarations hoist but do not initialize.
  • Calling functions before they are written works perfectly.

Up Next

Continue your journey with the next topic.

Go to JS Closures