2 min read

[3] Scope and More Fun With Variables

In the last section we briefly touched on the concept of scope and dive deeper into advanced syntax of variables .
[3] Scope and More Fun With Variables
Photo by Jakob Søby / Unsplash

In the last section we briefly touched on the concept of scope. Let's explore that further.

Understanding `scope``is foundational to understanding the logical flow of code.

Lets start by writing a few functions:

let someNumberAtGlobalScope = 35

function foo () {
    let anotherNumberAtFooScope = 100
    return anotherNumberAtFooScope + someNumberAtGlobalScope
}

function bar () {
    let someStringAtBarScope = 'One Hundred'
    return someStringAtBarScope
}

function baa () {
    let anotherStringAtBaaScope = 'Dollars'
    return anotherStringAtBaaScope + someStringAtBarScope // This will not work
}

If you think about the layout of your code as geography, you can think of scope as a region in your code. Every time you define a variable or a function, you are defining it in a particular scope.

In our example above, we see the variable someNumberAtGlobalScope. This variable is defined at the global scope of the program, meaning any bit of code after it as the ability to reference that variable. The reason this is defined at the global scope is because it does not live inside of any set of { }.

A set of { } is what defines any scope that is not global. In our function foo() we are defining a variable called anotherNumberAtFooScope. Inside of foo() we see that we are returning the value of anotherNumberAtFooScope + someNumberAtGlobalScope. The function foo() has access to the variable someNumberAtGlobalScope  because the function foo() is defined in the same scope as someNumberAtGlobalScope. In other works, foo() and someNumberAtGlobalScope are siblings in the code. The variable anotherNumberAtFooScope that is defined in foo() however, is not in the global scope and will cease to exist once the function foo() is done being executed.

Let's skip down to the function baa(). Ignore that we are working with variables that are not numbers for now. Here we are defining the variable anotherStringAtBaaScope and are trying to return the value of anotherStringAtBaaScope + someStringAtBarScope. The issue with this is that the variable someStringAtBarScope is not in a scope that is accessible to the function baa() As far as baa() is concerned, that variable does not exist nor has it ever existed.

So how do we get access to a value from a function. Well we need to return it. Now, bar() does return the value someStringAtBarScope, but we are not properly the return value.

Let's rewrite baa() with something like this:

function baa () {
    let anotherStringAtBaaScope = 'Dollars'
    return anotherStringAtBaaScope + bar() // This will not work
}

Here we are returning the value of anotherStringAtBaaScope plus the return value of the executed function bar(). Make note, that you can treat the execution of a function as if it the the actual value you are returning.