How can I check if a variable is empty in Javascript? Sorry for the stupid question, but I\'m a newbie in Javascript!
if(response.photo) is empty {
do so
Empty check on a JSON's key depends on use-case. For a common use-case, we can test for following:
nullundefined''{} [] (Array is an Object)Function:
function isEmpty(arg){
return (
arg == null || // Check for null or undefined
arg.length === 0 || // Check for empty String (Bonus check for empty Array)
(typeof arg === 'object' && Object.keys(arg).length === 0) // Check for empty Object or Array
);
}
Return true for:
isEmpty(''); // Empty String
isEmpty(null); // null
isEmpty(); // undefined
isEmpty({}); // Empty Object
isEmpty([]); // Empty Array