Global vs Local Variables question


Hello everybody, i have some questions about this task, Global vs Local variables,
I will add some comments:

the var my_number inside the function, this will have a local scope and will not be affected by the global scope variable yes? so inside the function, will my_number be 0 since it cant access the global variable, and when we write: timesTwo(7) it will return 14. But what is number? if the var my_number is local scope it doesnt have a value right?
if we just console log the my_number inside the function how does it return 14 if it cant access the global scope my_number? Maybe im making this into a big confusion but i hope maybe someone gets my point.

var my_number = 7; //this has global scope

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


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


I have written an answer for it...
That clears concept of global variable..

I hope it helps! :slight_smile:



number is the function parameter, when you call the function you supply 7 as argument, so number is 7. 7 * 2 is 14

but my_number inside the function (local variable) will be 14, the global variable will stay 7


Hi thank you for your reply!

I think i understood it now, when i call the:
It access the function, so 7 will be passed in to the local variable, which has a local scope and do 7 * 2 = 14, but when we run the console.log(my_number); outside the function it will return 7 because it dont have access to the function but it do have access to the global variable that was created outside the function?



both my_number inside the function and number (function parameter) are local variable, so when you call the function, 7 will be passed to local variable number.

You seem to understand fine :slight_smile:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.