0

Is the following true:

For something to be in the global scope means it can be accessed anywhere in all files.

All global variables have global scope.

Therefore, the global object (and its properties) is a type of global variable.

P.s. This is a genuine question I find it useful getting terminology down and maybe it might help me be able to read the docs later.

2 Answers2

-1

A JavaScript global variable is declared outside the function or declared with window object. It can be accessed from any function.

To declare JavaScript global variables inside function, you need to use window object.

window.value=90;  

Now it can be declared inside any function and can be accessed from any function. For example:

function m(){  
   window.value=100;//declaring global variable by window object  
}  
function n(){  
   alert(window.value);//accessing global variable from other function  
} 

When you declare a variable outside the function, it is added in the window object internally. You can access it through window object also. For example:

var value=50;  
function a(){  
   alert(window.value);//accessing global variable   
} 

You can read more here https://www.javatpoint.com/javascript-global-variable

user1665355
  • 3,324
  • 8
  • 44
  • 84
  • `window.value = …` is not a *declaration*. – Bergi Jul 26 '20 at 15:37
  • @Bergi Hm, it works for me, as in here. https://stackoverflow.com/questions/11148997/window-variablename – user1665355 Jul 26 '20 at 18:33
  • 1
    Sure it works and does create a property on the global object, but it does not *declare* anything. Given that the OP asked specifically about terminology, this is an important distinction. – Bergi Jul 26 '20 at 18:47
-2

All global variables can be used in all scripts. Just try it yourself. This is how jQuery or any library works.

Read more about global variables on Wikipedia.

Rojo
  • 2,749
  • 1
  • 13
  • 34