title | slug | tags | |||
---|---|---|---|---|---|
Global object |
Glossary/Global_object |
|
A global object is an {{glossary("object")}} that always exists in the {{glossary("global scope")}}.
In JavaScript, there's always a global object defined. In a web browser, when scripts create global variables defined with the var
keyword, they're created as members of the global object. (In {{Glossary("Node.js")}} this is not the case.) The global object's {{Glossary("interface")}} depends on the execution context in which the script is running. For example:
- In a web browser, any code which the script doesn't specifically start up as a background task has a {{domxref("Window")}} as its global object. This is the vast majority of JavaScript code on the Web.
- Code running in a {{domxref("Worker")}} has a {{domxref("WorkerGlobalScope")}} object as its global object.
- Scripts running under {{Glossary("Node.js")}} have an object called
global
as their global object.
The globalThis
global property allows one to access the global object regardless of the current environment.
var
statements and function declarations at the top level create properties of the global object. On the other hand, {{jsxref("Statements/let", "let")}} and {{jsxref("Statements/const", "const")}} declarations never create properties of the global object.
The properties of the global object are automatically added to the {{glossary("global scope")}}.
-
- {{glossary("global scope")}}
- {{glossary("object")}}
-
{{domxref("Window")}}
-
{{domxref("WorkerGlobalScope")}}