Global scope is a JavaScript function Scope.Scope determines the accessibility (visibility) of variables.
Variables defined inside a function are not accessible (visible) from outside the function.
A global variable is declared outside the function or declared with an object. It can be accessed from any function.
Declaring global variable within function
To declare global variables within a function, we need to use window object. For example:
1 | window.value=90; |
Now this value can declared with in any function and can be access and used from any function.
For example:
1 2 3 4 5 6 | function m(){ window.value=100;//declaring global variable by window object } function n(){ alert(window.value);//accessing global variable from other function } |
EXAMPLE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <html> <body> <script> function m() { window.value = 100; //declaring global variable by window object } function n() { alert(window.value); //accessing global variable from other function } m(); n(); </script> </body> </html> |
OUTPUT
1 | 100 |
Internals of global variable
On outside a function,when a variable declare, that will add in window object. So it can be access through window object.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <html> <body> <script> var value = 50; function a() { alert(window.value); //accessing global variable } </script> <input type="button" onclick="a()" value="get global"/> </body> </html> |
Global JavaScript Variables
If any variable declared outside a function, so that will becomes GLOBAL.
A global variable has global scope: All scripts and functions on a web page can access it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <!DOCTYPE html> <html> <body> <p id="jsdemo"></p> <script> var myName = "Ayan"; showName(); function showName() { document.getElementById("jsdemo").innerHTML = "My name is " + myName; } </script> </body> </html> |
OUTPUT
1 | My name is Ayan |
Automatically Global
If a value assign to a variable that has not been declared, it will automatically turn on GLOBAL variable.
This example will declare a global variable myName
, the value is assigned inside a function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <!DOCTYPE html> <html> <body> <p id="jsdemo"></p> <script> myFunction(); document.getElementById("jsdemo").innerHTML = "My name is " + myName; function myFunction() { myName = "Ayan"; } </script> </body> </html> |
OUTPUT
1 | My name is Ayan |
Global Variables in HTML
With JavaScript, the global scope is the complete
JavaScript environment.
In HTML, the global scope is the window object. All global variables belong to the window object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <!DOCTYPE html> <html> <body> <p id="jsdemo"></p> <script> var myName = "Ayan"; document.getElementById("jsdemo").innerHTML = "My name is " + window.myName; </script> </body> </html> |
OUTPUT
1 | My name is Ayan |
Global Properties
Property | Description |
---|---|
Infinity | A numeric value that represents positive/negative infinity |
NaN | “Not-a-Number” value, It means there is nothing like value. |
undefined | This property indicates that a variable will not assigned a value. |
Global Functions
Function | Description |
---|---|
decodeURI() | It will decodes a URI |
decodeURIComponent() | It will decodes a URI component |
encodeURI() | It will encode a URI |
encodeURIComponent() | It will encode a URI component |
escape() | It will use encodeURI() or encodeURIComponent() instead |
eval() | It willevaluates a string and executes it as if it was script code. |
isFinite() | It will determine whether a value is a finite, normal number |
Number() | It will convert an object’s value to a number |
parseFloat() | It will parse a string and returns a floating point number |
parseInt() | It will parse a string and returns an integer |
String() | It will convert an object’s value to a string |
unescape() | It will use decodeURI() or decodeURIComponent() instead |