Scope in JavaScript
Scope determines where variables are accessible in your code. It defines the 'visibility' and lifetime of variables – whether you can use a variable in a certain part of the program.
JavaScript has three main types of scope: global, function, and block. The rules changed with ES6 (let and const introduce block scope).
Global Scope
Variables declared outside any function or block are global – accessible everywhere in the code.
Global variables should be used carefully – they can cause conflicts in larger programs.
Function Scope
Variables declared with var inside a function are function-scoped – visible only inside that function.
Block Scope (let and const)
Variables declared with let or const inside {} braces (blocks like if, for, while) are block-scoped – only accessible inside that block.
var does NOT have block scope – it leaks out.
Scope Chain
When looking for a variable, JavaScript checks the current scope first, then the parent scope, up to global.
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.

