Global scope ! its not clear


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("Outside the function my_number is: ")

Scope, variables, functions

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.


I hope it helps! :slight_smile:

Global vs Local Variables question

thank you for the explanation :slight_smile:


Youre welcome! :thumbsup: :slight_smile:


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