JavaScript itself does not have a function to determine whether a variable is null, because variables may be string, object, number, boolean, etc. Different types require different determination methods. So I wrote a function in the article to determine whether the JS variable is empty. If it is undefined, null, '', NaN, false, 0, [], {}, and blank string, it will return true, otherwise it will return false.
function isEmpty(v) {
switch (typeof v) {
case 'undefined':
return true;
case 'string':
If (v.replace(/(^[ tnr]*)|([ tnr]*$)/g, '').length == 0) return true;
break;
case 'boolean':
If (!v) return true;
break;
case 'number':
If (0 === v || isNaN(v)) return true;
break;
case 'object':
If (null === v || v.length === 0) return true;
for (var i in v) {
return false;
}
return true;
}
Return false;
}
Test:
isEmpty() //true
isEmpty([]) //true
isEmpty({}) //true
isEmpty(0) //true
isEmpty(Number("abc")) //true
isEmpty("") //true
isEmpty(" ") //true
isEmpty(false) //true
isEmpty(null) //true
isEmpty(undefined) //true