Is there a standard function to check for null, undefined, or blank variables in JavaScript?-Collection of common programming errors
It’s really about what you need, I use something like:
self.empty = function () {
if (
element === "" ||
element === 0 ||
element === "0" ||
element === null ||
element === "NULL" ||
element === undefined ||
element === false
) {
return true;
}
if (typeof(element) === 'object') {
var i = 0;
for (key in element) {
i++;
}
if (i === 0) { return true; }
}
return false;
}
But, honestly, that’s a bit more broad than I would recommend for most, it just happened to evolve based on the integration needs with other people’s code over time. And, I’ll even note that it really doesn’t handle Arrays as well as it should. The best thing to do is give yourself some personal context and make sure you cover it.
Originally posted 2013-11-09 18:41:36.