Useful JavaScript Functions - Is Null or Empty
The below function will check if the passed variable is undefined, null an empty array or an empty string.
function isNullOrEmpty(variable) {
// If the passed value is undefined or null then we can return true
if(variable === undefined || variable === null)
return true;
// If the passed value is an empty array then we can return true
if(Array.isArray(variable))
return variable.length === 0;
// If the passed value is an empty string (once trimmed) then we can return true
if(typeof variable === "string")
return variable.trim().length === 0;
// If all the checks pass then we can return false
return false;
}