Last active
May 6, 2017 17:16
-
-
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
This file contains 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
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