Javatpoint Logo
Javatpoint Logo

JavaScript globalThis Object

JavaScript can access the global object using the global object property. The global object in JavaScript is called a window in a web browser. It is also referred global or web worker, Depending on the environment. The JavaScript frameworks with web servers use the globalThis property uniformly to get to the global object.

Why do we use the globalThis object?

  • Easy to developers: The various environments made it difficult to construct JavaScript code to run on several platforms. The environment, such as global objects, is referred to in various settings.
  • The setting of framework: Javascript framework with the help of the GlobalThis object is created to all of these issues address. We can now execute JavaScript code in multiple settings of the framework.
  • Windows and non-window environment: We can use global resources without creating additional checks and tests. We may now execute our function in a window or non-window environment.
  • Code execution: This object is quite helpful when unsure about the environment and code execution. It is helpful to make our code executable on several environments in an easy way.

Global keyword and its environment table

The following table shows the javascript output environment and its supported global keyword.

Javascript Environment Global keyword
Web Browsers this
Web Workers self
Node.js global

Syntax

The following syntax uses in the script tag to operate a global object.

It is directly used in the console and other output tabs.

Examples

The multiple examples show the javascript variable with the global data in the output tab.

Example 1

The following basic example shows gloabalThis variable with the process in the console tab.

Output

The image shows the output of the global object.

JavaScript globalThis Object

Example 2

The following basic example shows gloabalThis variable with other functions in the console tab.

Output

The image shows the output of the global object.

JavaScript globalThis Object

Example 3

The following basic example shows gloabalThis variable with the console tab.

Output

The image shows the output of the global object.

JavaScript globalThis Object

Example 4

The following basic example shows gloabalThis variable with the alert tab.

Output

The image shows the output of the global object.

JavaScript globalThis Object

Example 5

The following basic example shows gloabalThis variable with the onclick function.

Output

The image shows the output of the global object.

JavaScript globalThis Object

Example 6

The following basic example shows gloabalThis variable with the typeof function.

Output

The image shows the output of the global object.

JavaScript globalThis Object

Compatible Browsers

The javascript globalThis object is supported by the below browsers.

  • Chrome
  • Firefox
  • Opera
  • Edge
  • Safari

Conclusion

The "objectThis" is referred to javascript as ES11 or ES2020. It is available in ECMA Script 2020. The globalThis object is used to create cross-platform code, meaningful code that can run in various JavaScript environments.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA