Is there a universal JavaScript function that checks that a variable has a value and ensures that it's not undefined
or null
? I've got this code, but I'm not sure if it covers all cases:
function isEmpty(val){
return (val === undefined || val == null || val.length <= 0) ? true : false;
}
Source: Tips4all
You can just check if the variable has a truthy value or not. That means
ReplyDeleteif( value ) {
}
will evaluate to true if value is not:
null
undefined
NaN
empty string ("")
0
false
The above list represents all possible falsy values in ECMA-/Javascript. Furthermore, if you don't know whether a variable exists (that means, if it was declared) you should check with the typeof operator. For instance
if( typeof foo !== 'undefined' ) {
// foo could get resolved and it's defined
}
If you can be sure that a variable is declared at least, you should directly check if it has a truthy value like shown above.
Further read: http://typeofnan.blogspot.com/2011/01/typeof-is-fast.html
It's really about what you need, I use something like:
ReplyDeleteself.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.
function isEmpty(value){
ReplyDeletereturn (value == null || value.length === 0);
}
This will return true for
undefined // Because undefined == null
null
[]
""
and zero argument functions since a function's length is the number of declared parameters it takes.
To disallow the latter category, you might want to just check for blank strings
function isEmpty(value){
return (value == null || value.length === '');
}
// for null and undefined
ReplyDeletereturn typeof value === "undefined" || value === null;
You are a bit overdoing it. To check if a variable is not given a value, you would only need to check against undefined and null.
ReplyDeletefunction isEmpty(value){
return (typeof value === "undefined" || value === null);
}
This is assuming 0, "", and objects(even empty object and array) are valid "values".
If the variable hasn't been declared, you wont be able to test for undefined using a function because you will get an error.
ReplyDeleteif (foo) {}
function (bar) {}(foo)
Both will generate an error if foo has not been declared.
If you want to test if a variable has been declared you can use
typeof foo != "undefined"
if you want to test if foo has been declared and it has a value you can use
if (typeof foo != "undefined" && foo) {
//code here
}
you can use:
ReplyDeleteIf clause to validate if the string or value is not empty.
like this:
if (someVar.value)
{
//its not emppty
}
else
{
//Its empty
}