Created
February 3, 2015 20:51
-
-
Save andfaulkner/1a7402c91e30a37b8912 to your computer and use it in GitHub Desktop.
Comprehensive Javascript detector of whether a variable contains actual content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Comprehensive check for whether or not a Javascript variable has actual content | |
var hasContent = function(input) { | |
if (typeof input === 'number') { | |
if (isNaN(input)) { | |
return false; | |
}; | |
} else { | |
if (input === 0) { | |
return false; | |
} | |
} | |
if (typeof input === 'object') { | |
if(Object.getOwnPropertyNames(input).length === 0){ | |
return false; | |
}; | |
}; | |
if (input === null || input === {} || input === false | |
|| input === "" || input === undefined) { | |
return false; | |
} else { | |
return true; | |
}; | |
}; | |
//Usage | |
hasContent(someVariable); //returns true if someVariable has content |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment