10/13 Global vs. Local Variable


#1

I don't understand how inside the function my_number is: 14

var my_number = 7; //this has global scope

var timesTwo = function(number) {
var my_number = number * 2; <-----------------------------over here, how do you know what 'number' is equal to?
console.log("Inside the function my_number is: ");
console.log(my_number);
};


#2

@goldiefish,

var timesTwo = function(number) {
    // start of FUNCTION-BODY
    // =function= takes 1 PARAMETER =number=
    // this =number= PARAMETER is used a =local= VARIABLE
    // throughout the FUNCTION-BODY

    var my_number = number * 2; 
    // you created a =local= VARIABLE =my_number=

    console.log(
        "Inside the function =local= my_number is: " + my_number);
    console.log(
        "Outside the function =global= my_number is: " + window.my_number);

    // END of FUNCTION-BODY
};

Now if you call the timesTwo =function=
timeTwo()
and this timesTwo =function= was defined as having 1 parameter number
you will have to provide 1 argument
in our case a number Value 7
timesTwo( 7 );

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".**

var my_number = 7;
timesTwo( 7 );

[output]

Inside the function =local= my_number is: 14
Outside the function =global= my_number is: 7

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

Reference:

google search
== discussions / opinions ==
javascript global vs local variable site:stackoverflow.com
for instance
= http://stackoverflow.com/questions/2788159/turn-javascript-local-variable-into-global-variable
= http://stackoverflow.com/questions/19721313/javascript-local-and-global-variable-confusion


#3

@goldiefish,

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".**