Global scope ! its not clear


#1


https://www.codecademy.com/courses/javascript-beginner-en-6LzGd/2/2?curriculum_id=506324b3a7dffd00020bf661


here says the var outside the function is global! but in the example down the var outside the function but still effect on it ! is that because it's above the function?


var my_number = 7;
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);


Scope, variables, functions
#2

What is definition of Global variables in Javascript ?
In general statement I can put it like this > If a variable is
1. Accessed everywhere inside the code/webpage (for print/read)
2. and It can be modified
Then it is said to be Global variable.

So yes the function can change / modify the value of global variable unless if you create your own local variable inside the function.
for instance look at these examples..

and one more important behaviour ...

In terms of DOM...
Anything is global if it is direct child of Window object.
that...

But...

I hope it helps! :slight_smile:


Global vs Local Variables question
#3

thank you for the explanation :slight_smile:


#4

Youre welcome! :thumbsup: :slight_smile:


#5

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