🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEjavascript

javascript Documentation

LOADING ENGINE...

Hoisting

AI & DATA SCIENCE // hoisting

Hoisting moves declarations to the top of their scope before code runs. var and function declarations are hoisted; let/const are in the TDZ.

Syntax

console.log(x); // undefined (hoisted!)
var x = 5;

console.log(y); // ReferenceError (TDZ)
let y = 10;

Deep Dive Course

**Hoisting** is JavaScript's behavior of moving declarations to the top of their scope during the creation phase. **var** declarations are hoisted and initialized to `undefined`. **Function declarations** are fully hoisted (name and body). **let** and **const** are hoisted but not initialized — they're in the **Temporal Dead Zone** (TDZ).

1Understanding Hoisting

Hoisting is JavaScript's behavior of moving declarations to the top of their scope during the creation phase. var declarations are hoisted and initialized to undefined. Function declarations are fully hoisted (name and body). let and const are hoisted but not initialized — they're in the Temporal Dead Zone (TDZ).

💡

Function declarations are fully hoisted — you can call them before they appear in code. Function expressions are NOT hoisted.

editor.html
// var hoisting
console.log(myVar); // undefined (not ReferenceError!)
var myVar = 'hello';
console.log(myVar); // 'hello'

// Internally JS sees it as:
var myVar;           // hoisted to top
console.log(myVar);  // undefined
myVar = 'hello';
localhost:3000

2Practical Example

Here is a real-world application of Hoisting showing how it is used in production JavaScript code.

editor.html
// Function declaration - fully hoisted
greet(); // works! 'Hello, World!'

function greet() {
  console.log('Hello, World!');
}

// Function expression - NOT hoisted
// sayHi(); // TypeError: sayHi is not a function
const sayHi = () => 'Hi!';
localhost:3000

3Best Practices

Follow these guidelines when working with Hoisting:

1. Avoid relying on hoisting — declare before use

2. Use const/let to get TDZ protection against accidental early access

3. Understand hoisting to debug unexpected undefined values

⚠️

Tip: Function declarations are fully hoisted — you can call them before they appear in code. Function expressions are NOT hoisted.

editor.html
// var hoisting
console.log(myVar); // undefined (not ReferenceError!)
var myVar = 'hello';
console.log(myVar); // 'hello'

// Internally JS sees it as:
var myVar;           // hoisted to top
console.log(myVar);  // undefined
myVar = 'hello';
localhost:3000

Examples

Example 01Basic Usage
// var hoisting
console.log(myVar); // undefined (not ReferenceError!)
var myVar = 'hello';
console.log(myVar); // 'hello'

// Internally JS sees it as:
var myVar;           // hoisted to top
console.log(myVar);  // undefined
myVar = 'hello';
Example 02Advanced Example
// Function declaration - fully hoisted
greet(); // works! 'Hello, World!'

function greet() {
  console.log('Hello, World!');
}

// Function expression - NOT hoisted
// sayHi(); // TypeError: sayHi is not a function
const sayHi = () => 'Hi!';

Best Practices

  • Avoid relying on hoisting — declare before use
  • Use const/let to get TDZ protection against accidental early access
  • Understand hoisting to debug unexpected undefined values

Interview Question

What is the difference between var hoisting and function declaration hoisting?

Hint: Initialization vs full function body.

var declarations are hoisted and initialized to undefined. You can access the variable before its declaration but get undefined. Function declarations are fully hoisted — the entire function body is available before the declaration line. This is why you can call a function before it appears in code.

Exercises

MediumPractice using Hoisting in a real scenario.
View Solution
// var hoisting
console.log(myVar); // undefined (not ReferenceError!)
var myVar = 'hello';
console.log(myVar); // 'hello'

// Internally JS sees it as:
var myVar;           // hoisted to top
console.log(myVar);  // undefined
myVar = 'hello';

Frequently Asked Questions

What is the difference between var hoisting and function declaration hoisting?

var declarations are hoisted and initialized to undefined. You can access the variable before its declaration but get undefined. Function declarations are fully hoisted — the entire function body is available before the declaration line. This is why you can call a function before it appears in code.

Related Functions

VariablesLet-ConstScope-ContextFunction-Declaration