Hoisting in JavaScript

ÁmbitoCondicionales

Hoisting is JavaScript's behavior of moving variable and function declarations to the top of their scope during compilation – before code execution.

This means you can use functions and var variables before declaring them in code, but there are important differences with let/const.

Function Declaration Hoisting

Function declarations are fully hoisted – you can call them before defining.

var Hoisting

var declarations are hoisted, but initializations are not – the variable is undefined until assigned.

let and const – No Hoisting (Temporal Dead Zone)

let and const are NOT hoisted in the same way – accessing them before declaration causes ReferenceError (Temporal Dead Zone).

Function Expressions and Hoisting

Function expressions (including arrows) are not hoisted like declarations.

Best Practices

  • Always declare variables at the top of their scope.
  • Prefer let/const over var.
  • Use function declarations when order doesn't matter; expressions for conditional logic.

Quick Quiz

Hemos revisado y comprobado los materiales, pero aún pueden existir errores. El contenido se ofrece únicamente con fines educativos, así que úsalo bajo tu propia responsabilidad y verifica con otras fuentes si es necesario.

✨ Pregunta a Lara — tu compañera de estudio con IA

Desbloquea soporte de aprendizaje personalizado. Lara puede explicar lecciones, resumir temas y responder tus preguntas — disponible desde el plan Go y superiores.


Lara te ayuda a aprender más rápido — exclusivo para los miembros ReadyTools Go, Plus y Max.

Sigue Tu Progreso 🚀

Aprende más fácilmente siguiendo tu progreso completamente gratis.