JavaScript itself does not determine whether a variable is a null value function, because the variable may be string,object,number,boolean and other types, different types, the method of judgment is also different. So in the article wrote a function to determine whether the JS variable null value, if it is undefined, null, ', nan,false,0,[],{}, white string, all return True, otherwise return false
Copy Code code 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:
Copy Code code 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