Skip to content

Instantly share code, notes, and snippets.

@keyurgolani
Last active May 6, 2017 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keyurgolani/ce510817db7a7f060db0edf5970cd72a to your computer and use it in GitHub Desktop.
Save keyurgolani/ce510817db7a7f060db0edf5970cd72a to your computer and use it in GitHub Desktop.
Check if the object passed exists - JavaScript - Is not null, not undefined and not empty
exists = function(obj) {
if (typeof obj !== 'undefined') {
if (obj !== null && obj !== undefined) {
if (typeof obj === 'string') {
return obj !== '';
} else if (typeof obj === 'number') {
return obj !== 0;
} else {
/* Node.js supports JSON.stringify(obj) syntax.
* For other versions of JS, this should be replaced with appropriate syntax.
*/
return JSON.stringify(obj) !== '{}';
}
}
}
return false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment