Skip to content

Instantly share code, notes, and snippets.

@andfaulkner
Created February 3, 2015 20:51
Show Gist options
  • Save andfaulkner/1a7402c91e30a37b8912 to your computer and use it in GitHub Desktop.
Save andfaulkner/1a7402c91e30a37b8912 to your computer and use it in GitHub Desktop.
Comprehensive Javascript detector of whether a variable contains actual content
//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