Global vs Local Variables


#1

var my_number = 7; //this has global scope

var timesTwo = function(number) {
var my_number = number * 2;
console.log("Inside the function my_number is: ");
console.log(my_number);
};

timesTwo(7);

console.log("Outside the function my_number is: ")
console.log(my_number);

I do not understand how the my_number outside the formula (in the console.log(my_name)) is effected by adding the "var" before the my_number inside the formula. If you add the "var" as instructed it changes the console.log output for the "outside the function my_name is:" from 14 to 7. however the my_name variable is 7 and is global...its the one on the first line. So why does changing the in formula my_name change the out of formula my_name?


#2

@bemery,
If i run the code as is stated in your Post
in a windows 8.1 IE11 environment
i get the output

Inside the function my_number is: 
14
Outside the function my_number is: 
7

#3

That is the miracle you should observe in this exercise and the reason it is that easy to code.
The thing is that adding the var does in fact not change the value of the outer variable it helps it keeping its value.

But getting to the point. Without the var you have one variable in your entire code. So when you change the value of this variable inside the function that change is also applied to it outside of the function because it is global, as said one variable in the entire code.

By adding a var to the variable name in function you change this. Now you have 2 variables that share the name but nothing else. Inside the function my_number is seven not because the outer global my_number is seven but because of this statement:

timesTwo(7);

And when you change it's value inside of the function it stays there. Due to having the same name it is impossible to reach the global variable by its name. This is called shadowing.


#4

@bemery,

the FUNCTION talk

var myFunc = function( param1, param2) {
       //Begin of  anonymous FUNCTION-BODY
       //VARIABLE -myFunc- has an -anonymous function- assigned
       //this -anonymous function- has 2 PARAMETERS param1 and param2
       //param1 and param2 PARAMETERS are used 
       //as -local- VARIABLES throughout the FUNCTION-BODY

      console.log( param1 + " and " + param2 ) ;

      //End of anonymous FUNCTION-BODY
};

If you want to call/execute the anonymous function
you will have to add a pair of parentheses to the variable myFunc
like
myFunc();
As the anonymous function was defined
as having 2 parameters
you have to provide 2 arguments
in our case 2 string VALUES "Alena" and "Lauren"
like
myFunc("Alena","Lauren");

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**


#5

@bemery,

What you have to realise....

.is that, after starting your Browser you are entering the so-called

GLOBAL-scope . . .GLOBAL-bubble

and as you execute a FUNCTION, the code in the FUNCTION-BODY is executed in it's

LOCAL-scope . . . . LOCAL-bubble

=======================================

In this exercise you are declaring 2 variables in the GLOBAL-scope (outside of the NAP-function )

var tired = true;
var bored = false;

you can define a =function= which takes NO parameters
like

var nap = function() {
              //Begin of FUNCTION-BODY you are in the LOCAL-scope
          }

and from within the FUNCTION-BODY / LOCAL-scope you can reach out, into the GLOBAL-scope
where you can get access to Boolean VALUES of the variables tired and bored

============================

Now read the FUNCTION talk is in a separate POST

============================

Now you defined the nap =function= as having 2 parameters tired and bored
as these tired and bored parameters
are used as tired and bored LOCAL variables
you will NOT have access to the GLOBAL variables tired and bored

AND

as you are calling the nap =function= which has 2 parameters defined
you will have to provide 2 arguments,
otherwise the tired and bored varaibles within the LOCAL-scope are undefined

Conclusion::

Create the nap =function= with NO parameters.

Reference

google search
== the Book ==
javascript [your question] site:developer.mozilla.org
javascript logical operators site:developer.mozilla.org

== discussions / opinions ==
javascript [your question] site:stackoverflow.com

== guidance ==
www.crockford.com
http://javascript.crockford.com/code.html
http://javascript.crockford.com/survey.html
http://stackoverflow.com/questions/9549780/what-does-this-symbol-mean-in-javascript
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
www.developer.mozilla.org/en-US/docs/Web/JavaScript/Guide
http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname?rq=1