Created
April 7, 2018 03:10
-
-
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
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
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