Checking for JavaScript Variable Definitions#

In some cases you need to be able to check if a variable has been defined, and if it has a value.

I recently had a problem where I needed a function to check if a variable had been defined, and if not, to return a default value "1".

The following code will check if the variable is defined, and if it is, it will also check and make sure that it isn't null, before returning the value.

 

function GetDefaultModuleId(){
    if (typeof(csDefaultModule) == "undefined"){                            
        return "1";
    }else{
        if (csDefaultModule == null){
            return "1";
        }else{
            return csDefaultModule;
        }                    
    }
}

 

Categories:  | 
Tuesday, September 19, 2006 1:21:26 PM (Central Standard Time, UTC-06:00) #    Comments [0]  | 

 

Name
E-mail
(will show your gravatar icon)
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

All content © 2008, Christopher May, Inc
Open Job Positions
On this page
Google Ads
This site
Calendar
<November 2008>
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456
Archives
Sitemap
Blogroll OPML
Disclaimer

Powered by: newtelligence dasBlog 1.9.6264.0

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Send mail to the author(s) E-mail

Theme design by Jelle Druyts


Pick a theme: