Click to See Complete Forum and Search --> : undefined variable in javascript


sachin
06-30-2003, 02:12 PM
Hi,

I have a url from which i reterive name value pair in javascript.

var utest = oGetArgs.book;
var sBooktxt;
if (utest.indexOf("undefined") == -1)
sBooktxt = oGetArgs.book;
else
sBooktxt = "" ;

Even though when utest is defined, the sBooktxt is getting
set to "undefined" , but it has to get set to "" when utest is
undefined.

Regards,
Sachin

Jona
06-30-2003, 02:33 PM
if(typeof sBooktxt == "undefined"){
sBooktxt = "";
}


[J]ona

Charles
06-30-2003, 02:39 PM
"undefined" is evaluated as "false" in the boolean context in JavaScript and the "if" statement evaluates things in the booloean context. Hence, if (!sBooktxt) sBooktxt = ''.

Jona
06-30-2003, 02:58 PM
Well, what I posted was what first came to mind; although, what you said is the "better" way to do it.

Like I always say, I can always trust you to come back and provide a better way. ;)

[J]ona