Skip to content

Commit

Permalink
Shorten Global object glossary entry (#18474)
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena authored Jul 18, 2022
1 parent eef5e4d commit 5cda559
Showing 1 changed file with 3 additions and 42 deletions.
45 changes: 3 additions & 42 deletions files/en-us/glossary/global_object/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,11 @@ In JavaScript, there's always a global object defined. In a web browser, when sc
- 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`](https://nodejs.org/api/globals.html#globals_global) as their global object.

> **Note**: Unlike {{jsxref("Statements/var", "var")}}, the statements {{jsxref("Statements/let", "let")}} and {{jsxref("Statements/const", "const")}} do not create properties of the global object.
The [`globalThis`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis) global property allows one to access the global object regardless of the current environment.

## `window` object in the Browser
[`var`](/en-US/docs/Web/JavaScript/Reference/Statements/var) statements and [function declarations](/en-US/docs/Web/JavaScript/Reference/Statements/function) 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 `window` object is the Global Object in the Browser. Any Global Variables or Functions can be accessed as _properties_ of the `window` object.

### Access Global Variables

```js
var foo = "foobar";
foo === window.foo; // Returns: true
```

After defining a Global Variable `foo`, we can access its value directly from the `window` object, by using the variable name `foo` as a property name of the Global Object `window.foo`.

#### Explanation:

The global variable `foo` was stored in the `window` object, like this:

```js
foo: "foobar"
```

### Access Global Functions

```js
function greeting() {
console.log("Hi!");
}

window.greeting(); // It is the same as the normal invoking: greeting();
```

The example above explains how Global Functions are stored as _properties_ in the `window` object. We created a Global Function called `greeting`, then invoked it using the `window` object.

#### Explanation:

The global function `greeting` was stored in the `window` object, like this:

```js
greeting: function greeting() {
console.log("Hi!");
}
```
The properties of the global object are automatically added to the {{glossary("global scope")}}.

## See also

Expand Down

0 comments on commit 5cda559

Please sign in to comment.