Hoisting in JavaScript

From the JavaScript cheat sheet · Hoisting & Strict Mode · verified Jul 2026

Hoisting

Variable and function hoisting behavior

javascript
// Function hoisting
greet(); // Works!
function greet() {
  console.log('Hello');
}

// Variable hoisting
console.log(x); // undefined
var x = 5;

// let/const not hoisted
// console.log(y); // Error
let y = 5;
💡 Function declarations are fully hoisted and can be called before declaration
⚡ var declarations are hoisted but initialized as undefined
📌 let/const are in "temporal dead zone" until declaration
🔥 Always declare variables at the top of their scope to avoid confusion

More JavaScript tasks

Back to the full JavaScript cheat sheet