Can global javascript variables be created without assignment?

  • Thread starter Thread starter John Kotuby
  • Start date Start date
J

John Kotuby

Hi guys,

Can a global JS variable be declared by just using:

var theGlobalVar:

Or must here be an assignment:

var theGlobalVar = '';

Even if the variable wil later be used as something other than a string
object?
 
yes.
var myvar; //creates a variable who's value is undefined.

javascript has unusual scoping rules. the var usually only has meaning
if used in a function, in which case it declares it local to the function.

a = ""; // declare a global string
var a; // redundant a is still a string

function f1()
{
a = "f1"; // updates global a
b = "f1"; // creates global b
}
function f2()
{
var a = "l" ; // a is local to function
}

function f3()
{
a = "f3"; // a is local due to below
if (false)
{
var a; // defines a as local to function
}
}
function f4()
{
a = "f4" // global a
var c = function(){ var a = "anonymous"}; //local a
c();
}
function f5()
{
a = "f4" // global a
var c = function(){a = "anonymous"}; //global a via closure
c();
}
function f5()
{
var a = "f4" // local a
var c = function(){a = "anonymous"}; //f5's local a via closure
c();
}


-- bruce (sqlwork.com)
 
Back
Top