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
Az anyagokat átnéztük és ellenőriztük, de hibák továbbra is előfordulhatnak. A tartalom kizárólag oktatási célt szolgál, ezért saját felelősségre használd, és szükség esetén ellenőrizd más forrásokkal is.
✨ Kérdezd Larát — a tanulási partnered
Fedezd fel a személyre szabott tanulási támogatást. Lara elmagyarázza az anyagot, összefoglalja a témákat és megválaszolja a kérdéseidet — az Go csomagtól elérhető.
Lara segít gyorsabban tanulni — kizárólag a ReadyTools Go, Plus és Max tagoknak.

