Skip to content

Instantly share code, notes, and snippets.

@oskarnrk
Last active October 15, 2018 10:28
Show Gist options
  • Save oskarnrk/41eaa36a3b8449b37440a5e11ae53c14 to your computer and use it in GitHub Desktop.
Save oskarnrk/41eaa36a3b8449b37440a5e11ae53c14 to your computer and use it in GitHub Desktop.
Demonstration abount checking if a string is undefined, null or empty
/*
* 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