home

Quickies, by Andrea Olivato

text

[JS] How to test if a variable is defined

codepuzzling:

// returns 1 if defined, 0 otherwise.
function isDefined(anObj) {
   switch(typeof(anObj)) {
    	case "string": if(anObj != "") return 1; else return 0; 
    	case "undefined": return 0;
    	case "object": if(anObj != null) return 1; else return 0;
    	default: return 0;
   }
}

2 years ago

July 1, 2009
reblogged via codepuzzling
Comments (View)
blog comments powered by Disqus