Skip to content

Instantly share code, notes, and snippets.

@jwulf
Created April 7, 2018 03:10
Show Gist options
  • Save jwulf/5ff02073df5e6f213311cdcb0bfd1e6c to your computer and use it in GitHub Desktop.
Save jwulf/5ff02073df5e6f213311cdcb0bfd1e6c to your computer and use it in GitHub Desktop.
How to test if a variable is empty - including an empty object or empty array
function empty(data)
{
if(typeof(data) == 'number' || typeof(data) == 'boolean')
{
return false;
}
if(typeof(data) == 'undefined' || data === null)
{
return true;
}
if(typeof(data.length) != 'undefined')
{
return data.length == 0;
}
var count = 0;
for(var i in data)
{
if(data.hasOwnProperty(i))
{
count ++;
}
}
return count == 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment