Undefined Variable Detection

Results 1 to 3 of 3

Thread: Undefined Variable Detection

  1. #1
    Fraser Murrell Guest

    Default Undefined Variable Detection

    Hi,<BR><BR>Using JSCRIPT, I`m trying to detect if a variable is as yet undefined.<BR><BR>If I try :<BR><BR>var Mode;<BR><BR>if(Mode == undefined)<BR> Response.Write("Mode Undefined!");<BR><BR> .. I get a message back saying &#039undefined&#039 is undefined.<BR><BR>Is there a proper way of doing this?<BR><BR>Regards,<BR>Fraser

  2. #2
    Join Date
    Dec 1969

    Default RE: Undefined Variable Detection

    Yes, and it&#039s very simple: "undefined" in JavaScript evaluates to false in a boolean context (well, not true anyway), so you can do:<BR><BR>if(myvar) {<BR>// myvar is defined<BR>} else {<BR>// myvar is undefined<BR>}<BR><BR>The only problem is if you want to check to see if a boolean variable has been defined and set to false - since undefined gives the same result.<BR><BR>Dunc

  3. #3
    sigma Guest

    Default use typeof operator

    Try this<BR><BR>var Mode;<BR>if(typeof(Mode) == "undefined")<BR>Response.Write("Mode Undefined!");<BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts