The proper way is to use window object.
And use the syntax like this:
var window.iAmGlobal = "some val"; //Global variable declaration with window.
//Any place in other part of code
function doSomething()
{
alert(window.iAmGlobal); //I am accessible here too !!
//OR
alert(iAmGlobal); //I am accessible here too !!
}
By defining globals this way, you will make JavaScript more robust and reliable.
Note:
Define global variables with window keyword in form of “window.VAR_NAME”
You can access the variable with “window.VAR_NAME”
or directly “VAR_NAME”
Do not use other variables with same name otherwise it may cause unwanted results