> ## Documentation Index
> Fetch the complete documentation index at: https://docs.baenninger.me/llms.txt
> Use this file to discover all available pages before exploring further.

# Scope und Kontext

## Scope Konzepte﻿

**Scoping**: Wie die Variablen in unserem Programm organisiert und auf sie zugegriffen wird. "*Wo befinden sich die Variablen?* " oder *"Wo können wir auf eine bestimmte Variable zugreifen, und wo nicht?*"

**Lexical Scoping**: Das Scoping wird durch *Platzierung* von Funktionen und Blöcken im Code gesteuert

**Scope**: Raum oder Umgebung, in der eine bestimmte Variable deklariert wird (Variablenumgebung im Falle von Funktionen). Es gibt den *Global Scope*, den *Function Scope* und den *Block Scope*

**Scope einer Variable**: Bereich in unserem Code, in dem auf eine bestimmte Variable *zugegriffen* werden kann

## 3 Arten von Scopes﻿

### Global Scope﻿

```javascript theme={null}
const me = 'Jonas';
const job = 'teacher';
const year = 1989;
```

* **Ausserhalb** einer Funktion oder eines Blocks
* Im globalen Bereich deklarierte Variablen sind **überall** zugänglich

### Function Scope﻿

```javascript theme={null}
function calcAge(birthYear) {
  const now = 2037;
  const age = now - birthyear;
  return age;
}

console.log(now);
```

* Variablen sind nur **in Funktionen** zugänglich, nicht ausserhalb
* Gilt für **alle Funktionstypen**
* Auch **Local Scope** genannt

### Block Scope﻿

```javascript theme={null}
if (year >= 1981 && year <= 1996) {
  const millenial = true;
  const food = 'Avocado toast';
}

console.log(millenial);
```

* Variablen sind nur **innerhalb des Blocks** zugänglich
* Gilt nur für `let` und `const` Variablen
* Funktionen sind ebenfalls **Block Scoped** (im Strict Mode)
