JavaScript itself does not determine whether a variable is a null value, because variables may be of type string, object, number, boolean, etc. Different types and different judgment methods are also different. So I wrote a function in the article to determine whether the JS variable is null. If it is undefined, null, '', NaN, false, 0, [], {}, and blank strings, all return true, otherwise false
The code copy is as follows:
function isEmpty(v) {
switch (typeof v) {
case 'undefined':
return true;
case 'string':
if (v.replace(/(^[ /t/n/r]*)|([ /t/n/r]*$)/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:
The code copy is as follows:
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