betweenGo

Undefined

by Frank Kim on Feb.26, 2009, under JavaScript

Nouveau Variation on Flickr

(Photo: Nouveau Variation by Syntopia)

I find the undefined keyword and identity operators (=== and !==) in JavaScript pretty useful.  For example if I am parsing a JSON input and I am not sure if something is there or not I test it like this.

if (root.Foo === undefined)

If I want to test if something is defined I do it like this.

if (root.Bar !== undefined)

saladwithsteve explains it well in his JavaScript undefined vs. null post.

Update 12-14-2009: Unfortunately the above method did not work for testing a variable directly.  I ended up following the advice of this post, Javascript IsDefined Function.  To test if a variable is defined I now do this.

if (typeof(foo) != "undefined")


  • Share/Bookmark

Related posts:

  1. Submitting an ATG Form Using a Text Link
  2. Unobtrusive JavaScript
  3. JavaScript Invalid Argument in Internet Explorer Only
  4. Submitting a form with a radio button
  5. user-defined property type gotcha’s

:

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!