p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Javascript How-To (http://p2p.wrox.com/forumdisplay.php?f=87)
-   -   How to know a variable is undefined? (http://p2p.wrox.com/showthread.php?t=30399)

Edward King June 14th, 2005 04:08 AM

How to know a variable is undefined?
 
How to know if a variable is defined in Javascript? Which function I can use?

Thanks in advance.



interrupt June 14th, 2005 06:19 AM

Alert it by name?

<script language ="javascript">
var 1 = ("1")

alert(1)
</script>

The alert should return 1.

HTH
interrupt



ChrisScott June 14th, 2005 06:40 AM

You can use typeof...
Code:

if(typeof myVar == "undefined"){
    // variable is undefined, do something
}

However typeof will return "undefined" if a variable has not been set to any value / object / etc., so var myVar; would return "undefined" and var myVar = null; would not.

HTH,

Chris


Imar June 14th, 2005 10:14 AM

Also, 1 is not a valid variable name; it's a value.

var a1 = ("1")

alert(a1)

would work.....

Imar


All times are GMT -4. The time now is 11:34 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.