---
description: JavaScript's scope system is called "lexical scope"
---

# lexical scope

[JS](..) ⟩ [concepts](../../browser/concepts) ⟩ [scope]() ⟩ lexical scope

{% hint style="success" %}
:u6307: synonyms： "<mark style="color:purple;">**static scope**</mark>"

JavaScript's [](<> "mention") is determined at <mark style="color:yellow;">**compile time**</mark>; the term for this kind of scope is "<mark style="color:purple;">**lexical scope**</mark>". ("<mark style="color:purple;">**lexical**</mark>" is associated with the [lexing](../compile/lexing "mention") stage of compilation)

:point\_right: compare： [static-vs-dynamic](static-vs-dynamic "mention")
{% endhint %}

{% tabs %}
{% tab title="👥 相關" %}
* [](<> "mention") / [chain](chain "mention")
* [lexical-environment](../concept/execution-context/lexical-environment "mention")
{% endtab %}

{% tab title="⭐️ 重點" %}
{% hint style="info" %}
**JavaScript** is largely [lexically scoped](https://en.wikipedia.org/wiki/Scope_\(programming\)#Lexical_versus_dynamic_scoping), meaning that [func](../val/func "mention")s <mark style="color:yellow;">**can access**</mark> [variable](../variable "mention")s <mark style="color:yellow;">**defined in their surroundings**</mark>. 👉 see： [RingoJS](https://ringojs.org/documentation/module_and_global_scope/)
{% endhint %}

{% hint style="info" %}
* <mark style="color:purple;">**static scope**</mark>： C, Java, <mark style="color:yellow;">**JavaScript**</mark>, Python and Ruby.&#x20;
* dynamic scope： Perl.
{% endhint %}
{% endtab %}

{% tab title="📗 參考" %}
* [ ] [ydkjs-scope-and-closures-v.2](../../master/ref/book/you-dont-know-js-series-v.2/ydkjs-scope-and-closures-v.2 "mention") ⟩ Ch. 1
* [ ] Wikipedia ⟩ [lexical scope vs. dynamic scope](https://en.wikipedia.org/wiki/Scope_\(computer_science\)#Lexical_scope_vs._dynamic_scope)
* [ ] ringojs ⟩ [Understanding module and global scope](https://ringojs.org/documentation/module_and_global_scope/) ⭐️
* [ ] Anil ⟩ [Scope, Lexical Environment and Scope Chain in JavaScript](https://medium.com/@anilakgunes/scope-lexical-environment-and-scope-chain-in-javascript-559aadb7dca8) ⭐️
{% endtab %}
{% endtabs %}
