6

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?

11
  • 2
    Lol... No, this won't work. Write a helper function like this: function IsNullOrUndefined (obj) { return obj === null || typeof obj === 'undefined'; } Commented Jul 18, 2014 at 7:20
  • @WoIIe: "this won't work" -- what "this"? The second will. Commented Jul 18, 2014 at 7: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 ... Commented Jul 18, 2014 at 7: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. Commented Jul 18, 2014 at 7: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? Commented Jul 18, 2014 at 7:22

5 Answers 5

11

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.

Sign up to request clarification or add additional context in comments.

Comments

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

9 Comments

Why would you work with not declared variable? Isn't it stupid?
@zerkms Because I check if the variable is an undefined. undefined = not declared variable.
@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.
Exactly. Working with not declared variable doesn't make sense, but in my case I have to check if a variable is declared.
@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.
|
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.

4 Comments

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: "Except that if myVariable is not declared"
@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: 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.
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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.