DEV Community

benboorstein
benboorstein

Posted on • Edited on

Quick Notes Based On "Scope" Section of Frontend Masters' "Complete Intro to Web Development, v2"

What I did:

    function learnScopeBasics(num) {
       const sentence = 'Hey, shhh, I'm learning scope.'
       return 3 * num
    }

    console.log(learnScopeBasics(5)) // 15
    console.log(sentence) // Uncaught ReferenceError: sentence is not defined

Instead of the sentence being logged to the console, we get an error in the console: "Uncaught ReferenceError: sentence is not defined". This is because sentence in console.log(sentence) cannot "see" inside the learnScopeBasics function. In other words, the variable definition const sentence = 'Hey, shhh, I'm learning scope.' cannot be accessed from outside of the function in which that variable definition resides. If we were, however, to put console.log(sentence) inside of the function, then the sentence would get logged to the console.

And if we create an if statement inside of this function and put console.log(sentence) inside this if statement's code block, like this...

    function learnScopeBasics(num) {
       const sentence = 'Hey, shhh, I'm learning scope.'
       if (true) {
          console.log(sentence) // Hey, shhh, I'm learning scope.
       }
       return 3 * num
    }

    console.log(learnScopeBasics(5)) // 15

...then the sentence does get logged to the console. This is because, as a general rule, a variable definition in the outer scope can be accessed from an inner scope, but not vice versa.

A different example to illustrate this aspect of scope:

    let peopleAtTheParty = 0
    for (let i = 0; i <= 10; i++) {
       peopleAtTheParty++
    }

    console.log(i) // Uncaught ReferenceError: i is not defined

"Uncaught ReferenceError: i is not defined" is logged because console.log(i) is in the outer scope and is trying to access what's in the inner scope.

What I learned:

  • I learned that the below code logs what it logs. The first console.log() line I understood. Same with the second. But the third I did not.

    let globalVar

    function addFive(number) {
       globalVar = 'changed'
       return number + 5
    }

    console.log(globalVar) // undefined
    console.log(addFive(10)) // 15
    console.log(globalVar) // changed

What I still might not understand:

  • What I described just above, under "What I learned:".

    - An explanation from a mentor:

The first console.log statement: A global variable, globalVar, is declared, but not initialized. So it exists for reference anywhere in the script, and it doesn't have a value yet, thus it is 'undefined'.

The second console.log statement: This runs the function:

Inside the function, the existing global variable, globalVar, is mutated such that its value is now 'changed'.

There is no let, var, or const keyword in front of globalVar inside the function. Therefore, it is referencing the one declared in the global scope.

The third console.log statement: By the time this is run, globalVar's value has been changed to 'changed' due to the calling of addFive.


AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Tiger Data image

🐯 🚀 Timescale is now TigerData: Building the Modern PostgreSQL for the Analytical and Agentic Era

We’ve quietly evolved from a time-series database into the modern PostgreSQL for today’s and tomorrow’s computing, built for performance, scale, and the agentic future.

So we’re changing our name: from Timescale to TigerData. Not to change who we are, but to reflect who we’ve become. TigerData is bold, fast, and built to power the next era of software.

Read more

👋 Kindness is contagious

Explore this insightful write-up, celebrated by our thriving DEV Community. Developers everywhere are invited to contribute and elevate our shared expertise.

A simple "thank you" can brighten someone’s day—leave your appreciation in the comments!

On DEV, knowledge-sharing fuels our progress and strengthens our community ties. Found this useful? A quick thank you to the author makes all the difference.

Okay