Global scope; seems pretty confusing for a beginner

Hello fellow coders,

I’m having difficulty understanding global scopes. I get what they’re trying to achieve by reducing the block of codes written out, but it’s kinda hard reading the code top to bottom. Is the global scope some kind of giant memory? I’m sounding ridiculous by asking this, but I just something that could explain it like it would to a six year old.

If it helps a bit then you can think of it like it’s a large memory bank that everything can access. ‘Scope’ is essentially just referring to the places from which code can be accessed. Imagine the script like a hotel. So the whole building plus all it’s contents is your code file (you’re ‘test.js’ for example).
In a hotel, everything in the main lobby can be accessed by all of the guests and staff, as well as anyone who enters the building, this is your global scope. Just as everyone in the hotel can use the main lobby, every bit of code in the file can access things in the global scope (It’s usually best practise to declare all global variables at the beginning of the code so that you can easily locate them, hence the reading top to bottom).
Now you also have local scope. This would be your private guest rooms in the hotel. These can only be accessed by the guests that are staying in that room (ignoring staff for our purposes). In the real coding situation, this would be variables that have been declared inside functions (using the let keyword for example) and so just as anything in that room is only available to the guest, anything declared inside a function is only available in that function unless specifically returned.

Hopefully the analogy helps instead of making it more complicated! To summarise, the global scope essentially refers to ‘variables that can be accessed by anything in the code’.


Hi adamgaffney96,

I think I get it now, besides the global and local scope, the goal of scope is to make connections convenient rather than messy? Hence scope pollution? But also calling a function; would it be easier to call it on the end of the code?

Yeah exactly! So javascript is often used to make webpages more dynamic, and to do that you need to refer to specific elements on a webpage. Calling this 1000 times on a webpage can be tedious to type out, so instead you load it to a variable in the global scope, and then you can just call that variable, instead of a full line of code each time!

Scope pollution refers to having multiple different variables with the same name, and this can definitely happen when using the global scope, so as such you don’t want to always call a variable outside it’s function if it’s only to be used for that function. These are things you’ll pick up as you write more functions and programs.

For calling a function you can call it whenever, it depends on your purposes! I generally like to declare all global variables at the very top, then all function declarations, and then all function calls at the end. However sometimes you may need to use functions inside other functions, and so you need to pay attention to the order in which you are declaring them (NOTE: In other languages it literally won’t work if they’re not in the right order, in JS there’s something called hoisting which makes it work, but it’s still best practise and good habit to declare functions in the order they are used)