We can just check if the variable has a truthy value or not. That means
will evaluate to true if value is not:
- empty string ("")
If we can be sure that a variable is declared at least, we should directly check if it has a truthyvalue like shown above.
This will return true for
and zero argument functions since a function's
length is the number of declared parameters it takes.
To disallow the latter category, we might want to just check for blank strings
The first answer with best rating is wrong. If value is undefined it will throw an exception in modern browsers. We have to use:
We know this is an old question, but this is the safest check and we haven't seen it posted here exactly like that:
It will cover cases where value was never defined, and also any of these:
- undefined (value of undefined is not the same as a parameter that was never defined)
- "" (empty string)
P.S. no need for strict equality in typeof value != 'undefined'