Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
| Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old June 14th, 2005, 04:08 AM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.


  #2 (permalink)  
Old June 14th, 2005, 06:19 AM
Friend of Wrox
 
Join Date: May 2004
Location: , , .
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Alert it by name?

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

alert(1)
</script>

The alert should return 1.

HTH
interrupt


  #3 (permalink)  
Old June 14th, 2005, 06:40 AM
Friend of Wrox
 
Join Date: Jul 2003
Location: , , United Kingdom.
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

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

  #4 (permalink)  
Old June 14th, 2005, 10:14 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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

var a1 = ("1")

alert(a1)

would work.....

Imar


Similar Threads
Thread Thread Starter Forum Replies Last Post
Undefined Variable aspire Beginning PHP 4 July 12th, 2006 07:00 AM
Undefined variable najib Beginning PHP 4 July 1st, 2005 08:12 AM
undefined variable !! cody44 Beginning PHP 3 January 20th, 2005 05:47 PM
Undefined Variable fpolan Beginning PHP 9 July 15th, 2004 06:24 AM
Undefined Variable Some Help! knight Beginning PHP 7 February 19th, 2004 07:07 AM





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