Scope in JavaScript

FunktionsausdrückeHoisting

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

Wir haben die Materialien überprüft, dennoch können Fehler vorkommen. Der Inhalt dient ausschließlich Bildungszwecken, daher verwende ihn auf eigene Verantwortung und überprüfe ihn bei Bedarf mit anderen Quellen.

✨ Frag Lara — deine KI-Lernpartnerin

Entsperre personalisierte Lernunterstützung. Lara kann Lektionen erklären, Themen zusammenfassen und deine Lernfragen beantworten — verfügbar ab dem Go-Tarif.


Lara hilft dir, schneller zu lernen — exklusiv für ReadyTools Go-, Plus- und Max-Mitglieder.

Verfolge deinen Fortschritt 🚀

Lerne einfacher, indem du deinen Fortschritt kostenlos verfolgst.