JavaScript Hoisting

Beginner
6 min read
Updated: Nov 2026

Definition and Usage

Hoisting means declarations are processed before code runs. function declarations are fully available. var is declared early but starts as undefined. let/const are in the temporal dead zone until initialized.

JavaScript Syntax

JavaScript
console.log(typeof f);
function f() { return 1; }
console.log(f());
Try it Editor

How It Works

  1. typeof f. The function exists already.
  2. Call. Returns 1.

Examples

Example 1 — var is undefined

Declared but not assigned yet.

JavaScript
console.log(x);
var x = 2;
Try it Editor

How It Works

  1. Declaration hoisted; assignment stays in place.

Example 2 — let is not usable early

TDZ throws.

JavaScript
try { console.log(y); let y = 1; } catch (e) { console.log(e.name); }
Try it Editor

How It Works

  1. Access before init fails.

Example 3 — Declare first

Readable style.

JavaScript
function g() { return "ok"; }
console.log(g());
Try it Editor

How It Works

  1. Put declarations where readers expect them.

Frequently Asked Questions

Their bindings are created early, but you cannot use them before initialization (TDZ).
Like let—they are in the TDZ until evaluated.

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful