> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/concept/execution-context.md).

# execution context

[JS](/web/js.md) ⟩ [concept](/web/browser/concepts.md) ⟩ execution context

{% hint style="success" %}
An <mark style="color:purple;">**execution context**</mark> contains whatever <mark style="color:yellow;">**implementation specific state**</mark> is necessary to <mark style="color:yellow;">**track**</mark> the <mark style="color:yellow;">**execution progress**</mark> of its associated code.&#x20;

It may contain (but not limited to) the following：

* [lexical environment](/web/js/concept/execution-context/lexical-environment.md) - stores variables/this ...
* <mark style="color:yellow;">**code evaluation state**</mark> - state needed to perform/suspend/resume evaluation of the code associated with this execution context.

📘 [JS spec](/web/master/ref/js-spec.md) ⟩ 8.2 [Execution Contexts](https://262.ecma-international.org/6.0/#sec-execution-contexts)
{% endhint %}

{% tabs %}
{% tab title="⭐️ 重點" %}
{% hint style="info" %}
[function declaration instantiation](/web/js/val/func/declare/instantiation.md)
{% endhint %}

{% hint style="info" %}
[running execution context](/web/js/concept/execution-context/running-execution-context.md)

At any point in time, there is at most one execution context that is actually executing code. This is known as the *running* execution context.&#x20;
{% endhint %}

{% hint style="info" %}
[call stack](/web/js/concept/execution-context/call-stack.md)&#x20;

A stack is used to track execution contexts. The running execution context is always the top element of this stack.
{% endhint %}
{% endtab %}

{% tab title="🔴 主題" %}

* [running execution context](/web/js/concept/execution-context/running-execution-context.md)
* [call stack](/web/js/concept/execution-context/call-stack.md) - track execution contexts.
* [lexical environment](/web/js/concept/execution-context/lexical-environment.md) - part of execution context.
* [function declaration instantiation](/web/js/val/func/declare/instantiation.md)
  {% endtab %}

{% tab title="👥 相關" %}

* [lexical environment](/web/js/concept/execution-context/lexical-environment.md) is <mark style="color:yellow;">**part**</mark> of <mark style="color:purple;">**execution context**</mark>.
* [function declaration instantiation](/web/js/val/func/declare/instantiation.md) - the proccess <mark style="color:red;">**before**</mark>**&#x20;**<mark style="color:yellow;">**calling a function**</mark>.
* [scope](/web/js/scope.md) of a code block - can reach all its parents lexical environment.
* [scope chain](/web/js/scope/chain.md)
* [this](/web/js/concept/execution-context/this.md) is a <mark style="color:yellow;">**property**</mark> of <mark style="color:purple;">**execution context**</mark> <mark style="color:yellow;">**(**</mark>more specifically, [lexical environment](/web/js/concept/execution-context/lexical-environment.md)<mark style="color:yellow;">**)**</mark>.
  {% endtab %}

{% tab title="📗 參考" %}

* [x] Anil ⟩ [Scope, Lexical Environment and Scope Chain in JavaScript](https://medium.com/@anilakgunes/scope-lexical-environment-and-scope-chain-in-javascript-559aadb7dca8) ⭐️
* [ ] freeCodeCamp ⟩ [JavaScript Execution Context – How JS Works Behind The Scenes](https://www.freecodecamp.org/news/execution-context-how-javascript-works-behind-the-scenes/)
  {% endtab %}

{% tab title="📘 手冊" %}

* [ ] [JS spec](/web/master/ref/js-spec.md) ⟩ [Execution Contexts](https://tc39.es/ecma262/#sec-execution-contexts)
  {% endtab %}

{% tab title="🗣 討論" %}

* [What is the difference and relationship between execution context and lexical environment?](https://stackoverflow.com/questions/35759544/what-is-the-difference-and-relationship-between-execution-context-and-lexical-en)
* [Lexical environment and function scope](https://stackoverflow.com/questions/12599965/lexical-environment-and-function-scope)
  {% endtab %}
  {% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/web/js/concept/execution-context.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
