In my Javascript code I'm checking if a variable is undefined (not the value undefined but the variable not defined) or null. To trim the code I'm using an operator. This is how I do it:
if (myVariable === (undefined || null)) {
// Do something.
}
A friend of mine told me once, that I should rather split the checks into:
if (myVariable === undefined || myVariable === null) {
// Do something.
}
Is there really any difference between these two approaches? If yes, which one should I use and why?
In my Javascript code I'm checking if a variable is undefined (not the value undefined but the variable not defined) or null. To trim the code I'm using an operator. This is how I do it:
if (myVariable === (undefined || null)) {
// Do something.
}
A friend of mine told me once, that I should rather split the checks into:
if (myVariable === undefined || myVariable === null) {
// Do something.
}
Is there really any difference between these two approaches? If yes, which one should I use and why?
Share Improve this question edited Jul 21, 2014 at 15:20 user2226755 13.2k6 gold badges53 silver badges75 bronze badges asked Jul 18, 2014 at 7:11 dwettsteindwettstein 6871 gold badge7 silver badges17 bronze badges 11 | Show 6 more comments5 Answers
Reset to default 11Is there really any difference between these two approaches?
Yes.
myVariable === (undefined || null)
is equivalent to
myVariable === null
which is only true if myVariable is null, and false if myVariable is undefined. Whereas:
myVariable === undefined || myVariable === null
returns true if myVariable is either undefined or null.
If yes, which one should I use and why?
Neither (probably), even if the answer was yes. If you are trying to determine whether a variable exists or not, you can only test for global variables as they are properties of the global object:
// In global code
var window = this;
// Later…
if (varname in window) {
// varname is a global variable or property
}
Within a function execution context, you can only reliably test for a variable using try..catch:
try {
var blah = foo;
} catch (e) {
// foo is probably not a variable in scope
}
But that is almost certainly not a good idea. See JavaScript check if variable exists (is defined/initialized) - Which method is better?.
You should probably be doing:
if (typeof varname == 'undefined' || varname === null) {
// varname either does't exist or has a value of undefined or null.
}
The tests need to be in that order so that if varname hasn't been declared or otherwise created, the typeof test fails before the null test, which would otherwise throw an error.
Prefere : if (typeof myVariable === "undefined" || myVariable === null) {
.
variable === undefined vs. typeof variable === "undefined"
Because with if (myVariable === undefined) {
your console can be return an error or warning.
Like this :
ReferenceError: myVariable is not defined
if (myVariable === undefined) {
PS : (undefined || null)
is always null (because undefined return false).
===
operator in JS compares 2 operands (values).
In case of myVariable === (undefined || null)
the operands are: myVariable
, which represents the value it holds, and (undefined || null)
which represents the value null
, because operands (expressions) must be evaluated before comparison. And the (undefined || null)
expression is evaluated to null
.
So effectively your solution is identical to myVariable === null
.
If you follow the same idea and evaluate your friend proposal you will see that his advice is correct.
It's because (undefined || null)
always evaluates to null
so your first expression always false when myVariable
is undefined. The second variant is do what you want correctly.
Yes, it is. For your example, undefined
is equal false
then null
is equal false
too and this last value returns from expression. So this is why first approach is equal to if (myVariable === null) { ... }
. The second approach is preferable, but if you not a 'JavaScript: The Good Parts' guy, you can stick with if (myVariable == null) { ... }
or if (myVariable == undefined) { ... }
.
function IsNullOrUndefined (obj) { return obj === null || typeof obj === 'undefined'; }
– ˈvɔlə Commented Jul 18, 2014 at 7:20myVariable === undefined
doesn't check if myVariable is undefined. And since(undefined || true)
will always return false, he is just checkinf ifmyvariable === false
... – ˈvɔlə Commented Jul 18, 2014 at 7:22