What is the Global scope and what is the local scope?

Can someone explain this really simple?

When I try to look for the answere onto FCC and w3schools I can find but, can;t understand it:
Variables which are declared within a function, (this part I get but then)
as well as the function parameters have local scope. (what the heck does that even mean. I don’t understand it at all.) That means, they are only visible within that function. (which function do you mean?)

Then I tried w3schools:

Scope determines the accessibility (visibility) of variables. (yes but, which variabels and where does it do that? Can i find a vissible example in the code somewhere?)

JavaScript Function Scope

In JavaScript there are two types of scope:

  • Local scope
  • Global scope

JavaScript has function scope: Each function creates a new scope.(What the heck is a scope even)

Scope determines the accessibility (visibility) of these variables.(But why? where?)

Variables defined inside a function are not accessible (visible) from outside the function. (ok you lost me)

Examples:

let a = 1;

function example(para) {
  let b = 2;
  console.log(a);
  console.log(para);
  console.log(b);
}
example(3);
console.log(a);
console.log(b);
console.log(para);

What gets logged to the console:

a = 1
para = 3
b = 2
a = 1
b = undeclared
para = undeclared

Other cases:

let a = 1;

function example(para) {
  let b = 2;
  console.log(b);
  console.log(para);
}

function myFunc(para) {
  let b = 3;
  console.log(b);
  console.log(para);
}
example(4);
myFunc(5);
console.log(a);

Returned to the console:

b = 2
para = 4

b = 3
para = 5

a = 1

Hope this helps;

2 Likes

It helps a bit to understand what goes behind the code and how it get’s logged. But the diffrence between local scopes and global scopes seems still a mysterie to me.

A local scope is the area between two curly brackets { local scope }

Global scope is within the whole script. The variable is accessible anywhere the script is accessible.

2 Likes

Thanks for clearing this one up part;y. So what’s the diffrence between a scope, script. arguments and a parameter then? They all seem to go between the two curly brackets {}

You said that Global scope is within the whole script but, where in the whole script? And how do we deterimine it’s diffrence?

Sorry for asking so much but, I really want to understand them.

Scope is just a word we use describing how far reaching elements of code are.

When I say “script”, I am referring to the one page document containing the code.

Arguments are passed to a function, and said arguments are the parameters of a function. That is:

SCRIPT

let a = 0; // a is a global variable.

SCOPE

function myFunc(b) { // b is a parameter here.
  console.log("Hello");
}
let c = 2; // c is a variable here.
myFunc(c); // c is an argument here.

Depending on what you are wanting to refer to, scope is defined by you.

What I mentioned at the end of my last reply has to do with why it is dangerous (security-wise) to define important variables globally. That is, if someone has access to a reference of your script, they have access to the variables that are defined as having the whole script as their scope.

If a variable is defined in the script, outside of any curly-brackets, then it is global, and has scope of the whole script.
If a variable is defined in a script, inside curly-brackets, it has a scope less than that of the script.

1 Like