> 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/scope/global.md).

# global scope

[JS](/web/js.md) ⟩ [scope](/web/js/scope.md) ⟩ global

{% hint style="success" %}
(in a programming [environment](/web/js/concept/env.md)) \
the [scope](/web/js/scope.md) that <mark style="color:yellow;">**contains**</mark>, and is <mark style="color:yellow;">**visible**</mark> in, <mark style="color:orange;">**all other scopes**</mark>.
{% endhint %}

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

* [global object](/web/js/scope/global/object.md) - an [object](https://developer.mozilla.org/en-US/docs/Glossary/Object) that <mark style="color:orange;">**always**</mark> exists in the [global scope](https://developer.mozilla.org/en-US/docs/Glossary/Global_scope).
  * [globalThis](/web/js/scope/global/object/this.md) - identifier for the global object.
  * [window](/web/js/scope/global/object/window.md) - global object in browser.
  * [self](/web/browser/api/worker/self.md) - global object in web worker.
  * [global](/web/js/scope/global/object/global.md) - global object in Node.js.
  * [global object property](/web/js/scope/global/object/prop.md)
* [global variable](/web/js/scope/global/variable.md)
* [DOM element with "id"](/web/js/scope/global/dom-element-with-id.md)
* [isCurrentlyInGlobalScope()](/web/js/scope/global/is-in-global-scope.md) - check if current context is in global scope.
  {% endtab %}

{% tab title="⭐️ 重點" %}
{% hint style="danger" %} <mark style="color:purple;">**global scope**</mark>

* in <mark style="color:yellow;">**browsers**</mark>：<mark style="color:purple;">**global scope**</mark> <mark style="color:yellow;">**is**</mark> [top-level scope](/web/js/scope/top-level.md). &#x20;
* in <mark style="color:yellow;">**Node.js**</mark>：<mark style="color:purple;">**global scope**</mark> <mark style="color:red;">**is not**</mark> [top-level scope](/web/js/scope/top-level.md).

👉 Node.js ⟩ ["global"](https://nodejs.org/api/globals.html#globals_global) ⭐️
{% endhint %}

{% hint style="warning" %}
in the <mark style="color:purple;">**global scope**</mark>, [var](/web/js/variable/declare/var.md)/[function](/web/js/val/func/declare.md) <mark style="color:yellow;">**declarations**</mark> also expose themselves as [property](/web/js/val/obj/prop.md) on the [global object](/web/js/scope/global/object.md).
{% endhint %}

{% hint style="success" %}
in <mark style="color:purple;">**global scope**</mark>, <mark style="color:yellow;">**JS**</mark> exposes its <mark style="color:yellow;">**built-ins**</mark>:

* <mark style="color:yellow;">**primitives**</mark>: [undefined](/web/js/val/prim/undefined.md), [null](/web/js/val/prim/null.md), `Infinity`, `NaN`
* <mark style="color:yellow;">**natives**</mark>: `Date`, [Object](/web/js/val/builtin/object.md), [String](/web/js/val/prim/str.md) ...
* <mark style="color:yellow;">**global functions**</mark>: [eval](/web/js/scope/global/object/eval.md), `parseInt()`...
* <mark style="color:yellow;">**namespaces**</mark>: `Math`, `Atomics`, `JSON` ...
* <mark style="color:yellow;">**friends of JS**</mark>: `Intl`, `WebAssembly` ...

📗 [You Don't Know JS yet](https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/ch4.md#why-global-scope)
{% endhint %}

{% hint style="info" %}
each [environment](/web/js/concept/env.md) puts some of its own <mark style="color:yellow;">**built-ins**</mark> into the <mark style="color:purple;">**global scope**</mark>：

* [browser](/web/browser.md)：
  * <mark style="color:blue;">**`console`**</mark>
  * [DOM](/web/browser/dom.md)： [window](/web/js/scope/global/object/window.md), `document`,  ...&#x20;
  * <mark style="color:yellow;">**timers**</mark>：`setTimeout(..)`,  ...
  * <mark style="color:yellow;">**web APIs**</mark>：`navigator`, `history`, geolocation, WebRTC,  ...
* [web worker](/web/browser/api/worker.md)：??? (anything else?)
* [Node.js](/web/js/concept/env/node.js.md)：???
  {% endhint %}

{% hint style="info" %}
A "[variable](/web/js/variable.md)" is just a [property](/web/js/val/obj/prop.md) of the [environment record](/web/js/concept/execution-context/lexical-environment/environment-record.md) (associated with the <mark style="color:yellow;">**currently executing**</mark> [block](/web/js/grammar/statement/other/block.md) /[function](/web/js/val/func.md) /[module](/web/js/module.md) /<mark style="color:yellow;">**script**</mark>).&#x20;
{% endhint %}

{% hint style="warning" %}
Functions created with the [**Function**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function#difference_between_function_constructor_and_function_declaration) <mark style="color:yellow;">**constructor**</mark> <mark style="color:red;">**always**</mark> are created in the <mark style="color:purple;">**global scope**</mark>. :point\_right: [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function#difference_between_function_constructor_and_function_declaration)
{% endhint %}
{% endtab %}

{% tab title="🗺️ 圖表" %}

<div><figure><img src="https://ringojs.org/documentation/images/modulescope.png" alt=""><figcaption><p>✅ each module has its own scope </p></figcaption></figure> <figure><img src="https://ringojs.org/documentation/images/scriptscope.png" alt=""><figcaption><p>❌ global object shared by all scripts</p></figcaption></figure></div>
{% endtab %}

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

* [x] ringojs ⟩ [Understanding module and global scope](https://ringojs.org/documentation/module_and_global_scope/) ⭐️
* [ ] [YDKJS: Scope & Closures (v.2)](/web/master/ref/book/you-dont-know-js-series-v.2/ydkjs-scope-and-closures-v.2.md) ⟩&#x20;
  * [x] Ch. 3: [The Scope Chain](https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/ch3.md#chapter-3-the-scope-chain)
  * [ ] Ch. 4: [Around the Global Scope](https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/ch4.md#chapter-4-around-the-global-scope)
    {% endtab %}

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

* [ ] [Global scope](https://developer.mozilla.org/en-US/docs/Glossary/Global_scope)
* [ ] [Scope](https://developer.mozilla.org/en-US/docs/Glossary/Scope)
  {% endtab %}

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

* [What is the 'global' object in NodeJS](https://stackoverflow.com/a/52193294/5409815)
  {% endtab %}
  {% endtabs %}
