# Scope

The scope determines the accessibility to variables and functions. If something is "outside of scope" it means we can not access it.

![](https://4254577312-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MDnC_qISWkd4BnR0CPu%2Fsync%2F71f3c13c415389edeb08914344e08a10e30853ce.jpg?generation=1596439577834979\&alt=media)

## Global

The global scope is the scope that is accessible from anywhere. Anything that is defined outside of a function is a part of the global scope.

Consider this very simple program. It only contains a single variable called `planet`. The variable is a part of the global scope because it is defined outside any function.

```
var planet = 'earth';
```

## Local

However, if the variable had been defined inside a function instead it would no longer belong to the global scope.

```
function universe() {
    var planet = 'earth';
}
```

`planet` is no longer in the global scope, but instead constrained to the scope of the `universe` function.

```
function universe() {
    var planet = 'earth';
}

console.log(planet);
```

In other words, it is inaccessible from the global scope. Which in turn means that the code above would throw a `ReferenceError` stating that `planet is not defined`.

## Lexical

Another useful (but perhaps counterintuitive) scope is the lexical one. It provides a function within another function access to the scope of the outer function.

```
function universe() {
    var planet = 'earth';

    function milkyWay() {
        console.log\(planet\);
    }
}
```

Calling the `milkyWay` function would log out `earth` in the case above.
