4

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?

A-312
  • 10,203
  • 4
  • 38
  • 66
dwettstein
  • 577
  • 1
  • 4
  • 15
  • 2
    Lol... No, this won't work. Write a helper function like this: `function IsNullOrUndefined (obj) { return obj === null || typeof obj === 'undefined'; }` – WΩLLE - ˈvɔlə Jul 18 '14 at 07:20
  • @WoIIe: "this won't work" -- what "this"? The second will. – zerkms Jul 18 '14 at 07:21
  • @zerkms Just everything ... The check if a `myVariable === undefined` doesn't check if myVariable is undefined. And since `(undefined || true)` will always return false, he is just checkinf if `myvariable === false` ... – WΩLLE - ˈvɔlə Jul 18 '14 at 07:22
  • @Wolle—"that" won't work either. If a variable hasn't been declared or initialised, attempting to access it's value will throw an error in the call to your function. – RobG Jul 18 '14 at 07:22
  • @WoIIe: "The check if a variable === undefined won't work" --- why do you think so? What if you try before you continue this pointless discussion? – zerkms Jul 18 '14 at 07:22
  • @zerkms `var undefined = "lol"; var myVariable = "rofl"; if (myVariable === undefined) { }` ... Got it ? – WΩLLE - ˈvɔlə Jul 18 '14 at 07:24
  • @WoIIe: why would you redefine it? It makes no sense to do something stupid then use it as a justification for something else. – zerkms Jul 18 '14 at 07:25
  • @zerkms You just DON'T check the type without the `typeof()` function ... Just don't do it ... This is not correct. Since `undefined` is a type in JavaScript, you need to check the type of your variable and not the value -.-# – WΩLLE - ˈvɔlə Jul 18 '14 at 07:26
  • @WoIIe: "Since undefined is a type in JavaScript" --- it's not only a type, but also a *value*. "This is not correct" --- who told you so? " You just DON'T check the type without the typeof() function" --- why don't you do the same for `null` then? – zerkms Jul 18 '14 at 07:27
  • @zerkms The identity check `myVariable === undefined` would trigger the error "myVariable is not defined"... What's your damn problem? You just have to try it -.-# – WΩLLE - ˈvɔlə Jul 18 '14 at 07:30
  • @WoIIe: why would you work with a variable that is not declared? It's again - doing some stupid things to justify your opinion? "You just have to try it" --- I know how it works. I just don't see why you would do stupid things to make your life harder. I never redefine `undefined` and I never work with not declared variables. That is what professional programmers do, you should try it one day. – zerkms Jul 18 '14 at 07:31

5 Answers5

9

Is 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.

Community
  • 1
  • 1
RobG
  • 124,520
  • 28
  • 153
  • 188
3

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) {

enter image description here

PS : (undefined || null) is always null (because undefined return false).

Community
  • 1
  • 1
A-312
  • 10,203
  • 4
  • 38
  • 66
  • Why would you work with not declared variable? Isn't it stupid? – zerkms Jul 18 '14 at 07:35
  • @Hors: Thanks for your hint! – dwettstein Jul 18 '14 at 07:37
  • 1
    @zerkms Because I check if the variable is an undefined. undefined = not declared variable. – A-312 Jul 18 '14 at 07:37
  • 1
    @Hors Sujet: nope. My question: why would someone write a code that checks a variable that is not declared. It's stupid. If you don't declare a variable - then you don't need to check if it's undefined. It's a parse time check, no reason to check it in runtime. – zerkms Jul 18 '14 at 07:38
  • Exactly. Working with not declared variable doesn't make sense, but in my case I have to check if a variable is declared. – dwettstein Jul 18 '14 at 07:39
  • @dwettstein: cannot you just move your eyes up the code and tell if it's declared or not? That way you don't perform any checks in runtime. – zerkms Jul 18 '14 at 07:39
  • @zerms Maybe `myVariable` is an property of an object like this `obj.myVariable`. – A-312 Jul 18 '14 at 07:40
  • @Hors Sujet: if it's a property - then you use `'myVariable' in obj`, not comparison with `undefined` – zerkms Jul 18 '14 at 07:41
  • @zerkms: Unfortunatly I can't. I'm programming in vCenter Orchestrator and the variables can be declared on several locations. Basically, we use the check for an undeclared variable for debugging reasons. If an undeclared variable occur, we forgot to add it. – dwettstein Jul 18 '14 at 07:44
  • @zerkms And I don't know, it isn't my question. But the answer don't be return an error. – A-312 Jul 18 '14 at 07:44
2

=== 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.

zerkms
  • 230,357
  • 57
  • 408
  • 498
  • Except that if *myVariable* is not defined, then `myVariable === undefined` will throw an error (and for the paranoid, *undefined* might have been assigned some other value), whereas `typeof myVariable == 'undefined'` does more or less the same test but does not (and prevents `myVariable === null` from throwing an error too). :-) – RobG Jul 18 '14 at 07:41
  • @RobG: "Except that if myVariable is not **declared**" – zerkms Jul 18 '14 at 07:42
  • @zerkms—except that if *myVariable* has not been created by assignment, or declared, or included in formal parameter list. Is there a shorter expression? ;-) Maybe "if an identifier *myVariable* is not in scope"? – RobG Jul 18 '14 at 07:43
  • @RobG: if it is passed as a parameter - then `variable === undefined` will not throw an exception. It will only throw an exception if a variable **is not declared**. – zerkms Jul 18 '14 at 07:45
1

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.

Konstantin V. Salikhov
  • 4,204
  • 2
  • 33
  • 44
1

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) { ... }.