Tip Details Add a Related Tip

Rate as: Positive Negative

Detecting if a variable exists in Javascript

In Javascript there is no explicit function you can use to see if a variable exists. And to make matters worse, if you try to use a variable that does not exists already, you'll get an error in your script.

The easiest way to check for the existence of a variable in Javascript is to make use of the typeof operator as it can take undefined variables as input. The typeof operator will always return a string value, and if the value it is given is an undefined variable the string returned will be "undefined". So you can test for the existence of a variable like so:

if ( typeof(variableName) == "undefined" ) {
    // Action to take if variableName is not defined
} else {
    // Action to take if variableName *is* defined
}
Rating: 100% positive, 6 total Votes
Categories: javascript web programming coding
Added: on Jul 22, 2008 at 8:31 am
Added By: an anonymous user
Searches: variable javascript web exist coding

Comments on this Tip

Add a Comment
This method test if an existing variable is defined NOT if the variable exists. Checking typeof an undeclared variable gives an error.
– Added by an anonymous user on Sep 03, 2008 at 6:12 am
Worked perfectly for me! Wizard!
– Added by an anonymous user on Oct 21, 2009 at 2:29 pm
Your Comment:
(how to format)
Rate This Tip:

Verify Humanity:
Sorry, we know it's annoying, but please enter the characters shown in the image to the left so that we know you're an actual person and not an evil spammer. Thanks.
       

Related Tips

compressing javascript

100% positive, 0 comments
– Tip added by an anonymous user on Jun 12, 2008 at 3:02 pm

Static variables in Javascript

93% positive, 28 comments - last added on Nov 12, 2009 at 12:21 pm
– Tip added by an anonymous user on Aug 03, 2007 at 4:19 pm

Scrolling HTML elements using the keyboard

80% positive, 1 comment - last added on Jul 18, 2008 at 3:53 pm
– Tip added by rlansky on Jun 22, 2007 at 9:34 am

A better way to paginate through results

100% positive, 3 comments - last added on Nov 01, 2009 at 1:23 pm
– Tip added by tipstr on Mar 08, 2007 at 12:33 pm

Telling when an iframe is done loading

85% positive, 3 comments - last added on May 16, 2009 at 7:48 am
– Tip added by an anonymous user on Apr 18, 2008 at 8:05 am

Length of associative arrays in Javascript

100% positive, 0 comments
– Tip added by an anonymous user on Nov 04, 2008 at 10:13 am

Efficient building of large strings in Javascript

100% positive, 0 comments
– Tip added by an anonymous user on Aug 08, 2007 at 12:52 pm

reading select element values in javascript in IE7

100% positive, 0 comments
– Tip added by an anonymous user on Jul 17, 2009 at 1:28 pm

Unable to click in text input boxes on Internet Explorer

100% positive, 2 comments - last added on Aug 07, 2009 at 4:57 pm
– Tip added by Marcos84 on Dec 09, 2008 at 10:49 am

Optional parameters in javascript functions

100% positive, 0 comments
– Tip added by an anonymous user on Jul 30, 2008 at 10:44 am

Javascript variable naming

100% positive, 1 comment - last added on Jul 22, 2008 at 8:33 am
– Tip added by an anonymous user on Sep 30, 2007 at 7:28 am

getting rounded corners without using background images

100% positive, 0 comments
– Tip added by tipstr on Mar 13, 2007 at 10:35 am

Checking for undefined variables in Javascript

100% positive, 1 comment - last added on Jul 14, 2009 at 5:13 pm
– Tip added by an anonymous user on Jan 14, 2009 at 9:47 am

Code for creating Javascript CSS charts

100% positive, 0 comments
– Tip added by an anonymous user on Aug 21, 2007 at 12:23 pm

Creating vector graphics in javascript

100% positive, 0 comments
– Tip added by Marcos84 on May 10, 2007 at 2:44 pm

attaching events to nodes in Javascript

no ratings, 0 comments
– Tip added by an anonymous user on Aug 18, 2008 at 11:23 am

animating elements with jQuery

0% positive, 0 comments
– Tip added by an anonymous user on Aug 04, 2008 at 1:46 pm

Javascript: How to Create a Random Number

0% positive, 0 comments
– Tip added by Walkere on Feb 23, 2008 at 9:49 pm

Avoid invalid title names when opening javascript windows

no ratings, 0 comments
– Tip added by an anonymous user on Aug 07, 2007 at 9:36 am

Nice article on javascript techniques

no ratings, 0 comments
– Tip added by rlansky on Apr 24, 2007 at 8:12 am