Global in JavaScript

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:

Now this value can declared with in any function and can be access and used from any function.

For example:



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:

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. 


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.


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.


Global Properties

InfinityA numeric value that represents positive/negative infinity
NaN“Not-a-Number” value, It means there is nothing like value.
undefinedThis property indicates that a variable will not assigned a value.

Global Functions

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