Tell us what’s happening:
I am on Basic JavaScript: Global Scope and FunctionsPassed
I don’t understand the wording of the explanation:
In JavaScript, scope refers to the visibility of variables. Variables which are defined outside of a function block have Global scope. This means, they can be seen everywhere in your JavaScript code.
Seen? Seen by who? me!? Am I taking this too literally but I guess they mean the computer. If a variable is declared in a function then they can’t been seen by the computer.
Okay but what even is this? Down Below?
function fun2() {
var output = "";
if (typeof myGlobal != "undefined") {
output += "myGlobal: " + myGlobal;
}
if (typeof oopsGlobal != "undefined") {
output += " oopsGlobal: " + oopsGlobal;
}
console.log(output);
}
I don’t even understand half of what this is.
But it seems to say that if the two variables are undefined then they will output the outputs. such as “myGlobal: 10”
The output does show the statements that are to happen if the if statements come true so that must mean they are undefined but how?
But how is it undefined if it has a value in the output. Doesn’t that contradict?
Anyway this, at least for me, is just a terrible way of explaining this concept and is so frustratingly confusing. Maybe I am too tired. Your code so far
// Declare the myGlobal variable below this line
var myGlobal = 10;
function fun1() {
// Assign 5 to oopsGlobal Here
oopsGlobal = 5;
}
// Only change code above this line
function fun2() {
var output = "";
if (typeof myGlobal != "undefined") {
output += "myGlobal: " + myGlobal;
}
if (typeof oopsGlobal != "undefined") {
output += " oopsGlobal: " + oopsGlobal;
}
console.log(output);
}
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36.
Hi @k.williams1654. Not seen by you but by the rest of the code. In addition to @Anon551122’s explanation above, I suggest you take a look at the following resources. They might set you off regarding the concept of scopes.
I like to visualize it as a tree. Scope is relative to either global or an individual code block (such as a method or for loop). You can access less specific data than your current scope as long as it is a containing parent, not a sibling.
Global scope
Local scope 1
Local scope 1a
Local scope 2
Every scope has access to at least global, it is the primary parent scope. Local scope 1 could access global scope but NOT local scope 2 (another code block like a function)
Local scope 1a is a child of Local scope 1 and inherits the scope of both local 1 and global, however has no access to local 2 since it is not a direct descendant and has no correlation.
Remember this is one-sided. Local scope 1 would have no access to 1a’s scope, you can only inherit scope as a child.
Again, a scope is created whenever a code block is formed such as a function, loop, etc.
This helps to show why global scope variables should be avoided. The possibility of accidentally using a global variable that was referenced in a child scope can cause headaches.
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.