View Full Version : if var exists
Cattie
11-03-2003, 10:35 AM
I have the following problem.
I need to make a script that cheks if a variable exsists.
I tryed like this
if (trala = 'undefined'){
string = 'ja';
} else {
string = 'nee';
}
But my page gives a warning 'trala is undifened' and doesn't load the script.
What should I do?
Love Louise:p
fredmv
11-03-2003, 10:40 AM
if( typeof string == "undefined" )
{
string = "ja";
}
else
{
string = "nee";
}
liorean
11-03-2003, 03:58 PM
Could shorten it a bit...
string = (typeof trala != 'undefined')? 'nee' : 'ja';
fredmv
11-03-2003, 04:00 PM
Good call liorean, the conditional operator is a better way of doing things.
Robert T.
11-03-2003, 04:43 PM
There's a trick in JS that I find very helpful that is a variation of leorian's approach. By definition, any variable that is Null or Undefined returns false in JS (I learned this from Danny Goodman's book just recently, so I don't want to take any credit for this.) and it is also in the FAQ http://javascriptkit.com/javatutors/determinevar.shtml.
Try this...
(window.var ? "this variable has been defined" : "this variable has not been defined")
Written another way you could...
if (window.var)
{
statements if true
}
else
{
statements if false
}
HTH,
Robert T
liorean
11-03-2003, 04:48 PM
Yes, but using automatic typecasting of undefined to false on nonexistent objects is really bad form. It's okay with existing objects that are undefined, but not nonexistent objects.
That's why we use the typeof operator.
adios
11-03-2003, 04:57 PM
How 'bout....
function isset(sVarname)
{
return typeof window[sVarname] != 'undefined';
}
function lalalala()
{
if (isset('trala'))
str = 'ja';
else
str = 'nee';
}
liorean
11-03-2003, 05:01 PM
Isn't that a very complicated way of doing a simple thing? Besides, it ignores the possible existance of it anywhere else in the scope chain.
Jeff Mott
11-03-2003, 05:14 PM
but using automatic typecasting of undefined to false on nonexistent objects is really bad formAccording to who? It is often done in C and Perl as well as JavaScript, and I have never heard it described as bad form. In fact, it's usually said to be the better, more efficient, way.That's why we use the typeof operatorThat's a poor reason for using typeof then, given that you do not need to use typeof to avoid automatic typecasting.
liorean
11-03-2003, 05:48 PM
It's all theoretics anyway, but the thinking behind it is that undefined is a type, which has one value, undefined. An object should be declared for it to have a type and a value (if it doesn't exist, how could it have a type?) and thus objects that are not declared shouldn't even return the value undefined. ECMA was going this way with ECMA-262 4ed for some time, but Mozilla and Microsoft both decided that backwards compatibility was the way, and that made ECMA change their minds.
However, the thinking still holds in Strict Warnings mode for Mozilla.
(We'd also have seen a new construct handling just whether an object is declared or not, not whether it's defined or not, if ECMA had not succumbed to the preassure.)
Regarding of this... I feel often the need to separate the definition by the declaration and I don't know how...
I mean, sometimes I'd like to declare a variable, but give it no value, for the moment.
Othertimes I want just to verify if that variable exists, but I don't care about the value. I would like to be able to separate the existence of a variable by the existance of a value of that variable.
liorean
11-04-2003, 08:41 AM
There is actually a way to do that, but only in member space, not variable space. (note that global variables are in the global member space as well, while local variables are not in any member space)[string memberName] in [object parentObject]; // => exists?
// ECMA-262 3ed way of confirming existence in member space
// The value may be undefined, it will detect existence alone
[object memberObject] == undefined; // => defined?
// ECMA-262 way of confirming value being defined
typeof [object memberObject] != 'undefined'; // => exists?, and if so, defined?
// ECMA-262 way of confirming eistence and value being defined.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.