> For the complete documentation index, see [llms.txt](https://organization-bht.gitbook.io/it/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://organization-bht.gitbook.io/it/undefined-1/react-with-typescript/es6+/2.-arrow-function-this/javascript-execution-context.md). Agents can also search and read this documentation through its [MCP server](https://organization-bht.gitbook.io/it/~gitbook/mcp).

# Javascript Execution Context

**Execution Context**

실행 컨텍스트는 자바스크립트가 코드를 실행하기 전에\
작업 공간을 하나 마련해서, 변수와 함수, this를 다 준비하는 공간입니다.

이 공간이 **차곡차곡 쌓였다가, 실행이 끝나면 순서대로 정리되며 사라집니다.**

자바스크립트는 **싱글 스레드** 언어이며,\
**Call Stack**을 통해 **실행 컨텍스트(Execution Context)**&#xB97C; 순차적으로 처리합니다.

```javascript
function foo() {
  const a = 10;
  function bar() {
    const b = 20;
    console.log(a + b);
  }
  bar();
}
foo();
```

#### 실행 흐름:

1. **Global Execution Context 생성**
   * 변수: `foo`
   * 함수 선언: `foo`
   * `this` → window (브라우저 기준)
2. `foo()` 호출 → **Function Execution Context 생성**
   * 변수: `a`, `bar`
   * `this` → undefined (strict) 또는 window
3. `bar()` 호출 → 또 다른 Function Execution Context 생성
   * 변수: `b`
   * `this` → 마찬가지로 strict 여부에 따라 달라짐
4. 실행 컨텍스트는 \*\*Stack(스택 구조)\*\*로 쌓이고, \*\*LIFO(후입선출)\*\*로 처리됩니다

Stack 흐름:

* `Global Context` 생성 → push
* `a()` 호출 → `a Context` push
* `b()` 호출 → `b Context` push
* `console.log()` 실행
* `b Context` pop
* `a Context` pop
* `Global Context` 유지 (브라우저가 종료될 때까지)
