Last active
October 15, 2018 10:28
-
-
Save oskarnrk/41eaa36a3b8449b37440a5e11ae53c14 to your computer and use it in GitHub Desktop.
Demonstration abount checking if a string is undefined, null or empty
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
/* | |
* https://codereview.stackexchange.com/questions/5572/string-isnullorempty-in-javascript | |
* Thanks to ndp (https://codereview.stackexchange.com/users/8041/ndp) | |
*/ | |
// Starting with: | |
return (!value || value == undefined || value == "" || value.length == 0); | |
// Looking at the last condition, if value == "", it's length MUST be 0. Therefore drop it: | |
return (!value || value == undefined || value == ""); | |
// But wait! In JS, an empty string is false. Therefore, drop value == "": | |
return (!value || value == undefined); | |
// And !undefined is true, so that check isn't needed. So we have: | |
return (!value); | |
// And we don't need parentheses: | |
return !value | |
// Q.E.D. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment