Skip to content

Instantly share code, notes, and snippets.

@Alex1990
Created August 18, 2014 01:41
Show Gist options
  • Save Alex1990/d54917f1c3f5f3a8ce1e to your computer and use it in GitHub Desktop.
Save Alex1990/d54917f1c3f5f3a8ce1e to your computer and use it in GitHub Desktop.
Check if a string is empty (not contains any characters)
/*
* Ref: http://stackoverflow.com/questions/154059/how-do-you-check-for-an-empty-string-in-javascript
*/
// `str` must be a string
function isEmpty(str) {
return !!str.length;
}
// check if the `str` is empty, null or undefined (`str` may be 0 or false)
function isEmpty(str) {
return !str;
}
// check in `if` statement
// `str` may be a empty string, null, undefined, 0 or false
if (str) {
// statement...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment