Local Scope and Functions HELP!

Local Scope and Functions HELP!
0

#1

I am so beyond confused on this part it’s not even funny so can some please explain to me exactly what I am suppose to do and then tell me why what I am doing is wrong?

Instructions
Declare a local variable myVar inside myLocalScope. Run the tests and then follow the instructions commented out in the editor.


function myLocalScope() {
  'use strict';
  var myVar = "Hello";
  
  console.log(myVar);
}
myLocalScope();

// Run and check the console
// myVar is not defined outside of myLocalScope
console.log(myVar);

// Now remove the console log line to pass the test

I get two different errors when doing this.

  1. global myVar is not defined but it never says to make a global variable in the instructions
    If I were to make a global myVar variable it would look just like this right?
    myVar = “5”;

  2. Reference Error: myVar is not defined.
    But didn’t I define it?
    var myVar = “Hello”;
    Isn’t creating the variable and assigning it the value of Hello defining the variable?
    I am so utterly confused at this point.

EDIT I passed the challenge by deleting:

// Run and check the console
// myVar is not defined outside of myLocalScope
console.log(myVar);

// Now remove the console log line to pass the test.

But I am still confused on this whole thing though I feel I passed the challenge purely on luck


#2

Every function creates a new scope. That means that anything you define in the function, it’s existence is limited to the functions body.

So if I have, say a function foo, and a variable ‘testvar’, then ‘testvar’ only exists for the duration of the foo’s existence, and no function or variable outside foo knows about what ‘testvar’ is.

However, functions and variables inside foo do know what ‘testvar’ is, as they are in the same scope.

For example:

function foo(){
var testvar = 55;
var b = testvar + 1;
console.log(testvar, b); //outputs 55, 56. This is the local scope of foo.
}

console.log(testvar); //outputs undefined because testvar is only known to foo, and this is outside its scope! This is the global scope.

Now the challenge simply wants to understand this difference. If you wanted a global variable, you could have simple used var myVar= whatever, outside of any functions.

You defined myVar as ‘hello’ INSIDE a function, and thus everything outside it, including the global scope, has no idea what myVar means.


#3

Ok so this is what I understand now. This function

function myLocalScope() {
  'use strict';
  var myVar = "Hello";
  
  console.log(myVar);
}
myLocalScope();

Is like the inside of a box and var MyVar = “Hello”; is a local variable that only applies to things within the box. But if I made a global variable such as myVar = “55” that would affect everything outside and inside the box. And the challenge wanted me to know the difference between both but only want me to make a local variable that affects whats in side the box right?

Also can you clarify the difference between a local and global variable?

I think a global variable is written as

myVar = "Global";

and global variables affect everything regardless of being inside a function or string or w.e be the case right? They also don’t need to have var written in front right of them?

and a local variable needs to have var written in front and only affects things within its scope right?

var myVar = "Local";

And finally can you give me some clarification on scope?
I think I understand scope as being a house so an entire project would be the global scope or the entire house. And then local scopes would be each room or functions within the house. Then you can add to each scope locally or globally right? So if the whole house needed electricity you would add houseVar = “Electricity”; to function globalHouse() and that would give the whole house electricity which means every room in the house obviously has electricity now since its global right? But if you needed to just say paint the kids room blue. You would use function localRoom() and a local variable such as var roomVar = “blue paint”; since you only want that one room painted and not the entire house right?


#4

Your analogy is pretty spot on, scopes can definitely be thought of as boxes or rooms inside a larger box or house respectively. Of course, those boxes can have boxes inside them and so on…

As for defining global variable, you just do it the same way you define any variable, it’s just outside ANY function. So you still use the ‘var’ keyword to define it.

All that changes is that the location of definition of the variable is different. Think of the global scope as another (really big) box, and all the function inside it can access anything in there.

Using your code:

var globalVar = "I am global!";

function myLocalScope() {
  var myVar = "I am local!";
  console.log(myVar, globalVar); //I am local, I am global
}

myLocalScope();
console.log(myVar, globalVar); //undefined, I am global

//Let's try this with another function
function anotherFunction(){
    //change what myVar means
    var myVar = "I am inside another function";
   
   //Lets try a function inside another function
   function innerFunction(){
        var anotherVar = "I am inside innerFunction":
        console.log(myVar, globalVar); 
        //I am inside another function, I am global
        console.log(anotherVar); //I am inside inner function
    }

    innerFunction();
    
    //Let's try printing everything again
    console.log(myVar, globalVar); 
    //I am inside another function, I am global
    console.log(anotherVar); //undefined
}

anotherFunction();

Some things to remember:

Hope this helps!


#5

I think I understand at least variables and scopes now. I might get a little confused with other stuff later on but when the time comes I’ll ask plenty of other questions :D! Thank you veru much for helping me!


#6

HI! I realy don’t understand why this doesn’t work for me.
can someone explain?
I read, watch here and there to understand, but at some point I could give up after many tries, when things work for others and not for me.
does Fcc interface works right? am I wrong? , before I quit and break my computer, I call for help!

function myLocalScope() {
‘use strict’;
var myVar;

console.log(myVar);
}
myLocalScope();


#7

finally I found out.

But , realy, isn’t that Javascript somehow reshaped from a few older version, so much reshaped that it doesn’t explain anything?
how come we discover the functions as we are asked to find a solution, that we don’t efffectively know, and never knew before?

is it just so logic and I’m so dumb that it is normal I feel completely lost in this course ?

if this course is notproperly updated it could make some people 'like me )to abandon