# #10, why are they both 14 when left as-is?

#1

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>

```

var my_number = 7; //this has global scope

var timesTwo = function(number) {
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);

``````<do not remove the three backticks above>

I understand the difference between outside and local variables. Local variables only work inside the function and no where else.

So why would the outside function return 14 as well? Since "my_number" has global scope, wouldn't "console.log(my_number)" just return 7? "my_number" hasn't been defined with "var my_number" anywhere to be 14.
It just said that the var for timesTwo within the function should take my_number and times it by 2.

I understand what the correct answer should be but I don't understand why the wrong answer is wrong.``````

#2
``````var my_number = 7; //this has global scope

var timesTwo = function(number) {
//reaching out to the =global defined= VARIABLE
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);

console.log("=============================");

var my_number = 7; //this has global scope

var timesTwo = function(number) {
//define its own =local= defined =my_number= VARIABLE
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);
``````

[output]

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

#3

Why is the outside function the only one what changes? I would think that the outside function number would have no changes since it is only calling the global variable, which I would assume wouldnâ€™t be affected by a local variable use of the variable.

#4

@uselesshappy,
In the second option

``````var my_number = 7; //this has global scope

var timesTwo = function(number) {
//define its own =local= defined =my_number= VARIABLE
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);
``````

You first define a my_number variable =outside= of your FUNCTION
as a =global= variable
and
then within the FUNCTION, with var my_number = number * 2;
you have defined a my_number variable which is =local= to the FUNCTION
and the FUNCTION did NOT access the =global= variable

#5

Are these different variables even though they share the same name? Is var my_number, the global variable, a completely different variable from var my_number, used in the function, a local variable?

#6

Why was the global variable not accessed when var isnâ€™t specified? I have to agree with @uselesshappy, this is confusing and the explanation isnâ€™t really sufficient.

#7

Wait, maybe I understand now. Youâ€™re saying that if var isnâ€™t used within the local function then the global variable gets used, and processed by the function, in both cases (very unintuitive).

When var is specified it creates a separate variable, and the function ignores the global variable, therefore it remains unprocessed/unmodified by the function and remains 7.