Skip to content

Instantly share code, notes, and snippets.

@fyulistian
Created July 16, 2018 10:21
Show Gist options
  • Save fyulistian/4e490a33cfc4b98b0db44968a788a5e4 to your computer and use it in GitHub Desktop.
Save fyulistian/4e490a33cfc4b98b0db44968a788a5e4 to your computer and use it in GitHub Desktop.
Checking empty object, empty string, undefined by jQuery
// function for check empty by data type
var is_empty = function(data) {
// data type object
if (typeof(data) === 'object') {
// check empty for data object
if (JSON.stringify(data) === '{}' || JSON.stringify(data) === '[]'){
return true;
} else if(!data) {
return true;
}
return false;
// data type string
} else if (typeof(data) === 'string') {
// check empty for data string
if (!data.trim() || data == "" || data == null){
return true;
}
return false;
// data type undefined
} else if (typeof(data) === 'undefined'){
return true;
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment