Global variables in Javascript

by Ramya 2010-06-17 15:52:26

Global variables in Javascript:

Normally we declare global variables outside all functions as,
var test = 5;
function result(){
var calc = parseInt(test)+5; //Something like that......
}


But if you want to define a variable as global inside a function then use the following steps:
* First, declare the variable as,
var Globalvar = value;

* Then use make it as global by using the keyword "window" as,
window.Globalvar = value;

for eg,
function test(){
var Globalvar = somevalue;
window.Globalvar = somevalue;
}
function test1(){
alert(window.Globalvar);
}

Tagged in:

945
like
0
dislike
0
mail
flag

You must LOGIN to add comments