JavaScript itself does not determine whether a variable is a null value, because the variable may be of the string, object, number, boolean type, and other types, and the judgment method is 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, [], {}, blank string, true is returned; otherwise, false is returned.
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:
isEmpty() //trueisEmpty([]) //trueisEmpty({}) //trueisEmpty(0) //trueisEmpty(Number("abc")) //trueisEmpty("") //trueisEmpty(" ") //trueisEmpty(false) //trueisEmpty(null) //trueisEmpty(undefined) //true
Refer:
Http://blog.csdn.net/mycwq/article/details/17791633