14

I want to guard my functions against null-ish values and only continue if there is "defined" value.

After looking around the solutions suggested to double equal to undefined: if (something == undefined). The problem with this solution is that you can declare an undefined variable.

So my current solution is to check for null if(something == null) which implicetly checks for undefined. And if I want to catch addionalty falsy values I check if(something).

See tests here: http://jsfiddle.net/AV47T/2/

Now am I missing something here?

Matthias

Community
  • 1
  • 1
luebken
  • 6,221
  • 5
  • 22
  • 18

4 Answers4

14

The standard JS guard is:

if (!x) {
    // throw error
}

!x will catch any undefined, null, false, 0, or empty string.

If you want to check if a value is valid, then you can do this:

if (Boolean(x)) {
    // great success
}

In this piece, the block is executed if x is anything but undefined, null, false, 0, or empty string.

Travis Webb
  • 14,688
  • 7
  • 55
  • 109
  • And how would you guard against null and undefined? And nothing else! – luebken Mar 17 '11 at 14:13
  • You'd have to explicitly check for those two cases, e.g., `(x !== undefined && x !== null)`. Whenever checking null/undefined, make sure you use the `!==` (identity) operator and not the `!=` (equality) operator. – Travis Webb Mar 17 '11 at 14:41
  • and why doesn't 'if(x == null)' work as a guard against undefined and null? – luebken Mar 17 '11 at 16:35
  • That will work for "guard" purposes, but `undefined` and `null` have distinct values in javascript so when I write code I always want to illustrate use of correct semantics. A variable can be defined and have a value of null; so the variable is null, but *not* undefined. If you get in the habit of comparing variables to `null` to see if they are defined, you may run into issues for certain corner cases. In other words, IMO this is bad practice, and better safe than sorry. – Travis Webb Mar 17 '11 at 17:53
  • 1
    both !x and x !== undefined blow up on an undefined var with x is not defined. The typeof check is the only one I know that doesn't throw an exception on an undefined variable. – David Eison Jul 15 '11 at 23:32
  • @DavidEison not "undefined", you mean "undeclared". Besides the Error says "_someUndefinedVarName is not defined" in that case, it means "undeclared". – Juanma Menendez Mar 29 '19 at 16:39
  • `if (x)` is exactly the same as `if( Boolean(x) )` fyi – James Jul 08 '19 at 12:08
9

The only safe way that I know of to guard against really undefined variables (meaning having variable name that were never defined anywhere) is check the typeof:

if (typeof _someUndefinedVarName == "undefined") {
   alert("undefined");
   return;
}

Anything else (including if (!_someUndefinedVarName)) will fail.

Basic example: http://jsfiddle.net/yahavbr/Cg23P/

Remove the first block and you'll get:

_someUndefinedVarName is not defined

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
  • 1
    jQuery ensures that `undefined` really is `undefined` and not some variable by doing this: `function(undefined) { ... })()`. Inside that closure, `undefined` is safe. Since no arguments are passed to this function but it has a signature of one parameter, the `undefined` argument will always be undefined, for real. I recommend this technique over yours both for cleanliness and efficiency. If the programmer is dumb enough to mess with `undefined` themselves inside that closure and then wonder why it doesn't work, that's their fault... – Travis Webb Mar 17 '11 at 14:48
2

Only recently discovered using '&&' as a guard operator in JS. No more If statements!

var data = {
  person: {
    age: 22,
    name: null
  }
};

var name = data.person.name && doSomethingWithName(data.person.name);
Rich
  • 882
  • 1
  • 8
  • 19
  • 2
    Yes but: a) For be completely sure you need to check every property existence one by one, and even the root object: **data && data.person && data.person.name** b) If the object "data" is undeclared an error will be thrown. **Conclution:** You have to be cautious with the guardian operator because it is not a silver bullet – Juanma Menendez Mar 29 '19 at 16:44
  • 2
    No. If the name is empty string or zero, your code will fail. Use the nullish coalescent operator that will be available soon: `data.person.name ?? doSomethingWithName(data.person.name)` –  Oct 10 '19 at 02:36
  • @JuanmaMenendez you're right, like most things in JS, you need to be careful when using it. – Rich Oct 22 '19 at 23:18
2

Ternary to the rescue !

(i) => 
    i == 0 ? 1 :
    i == 1 ? 2 :
    i == 2 ? 3 :
    null
  • 7
    Ternary may be useful, but nested it tends to decrease readability. It is best practice in many places to avoid nesting ternary operations. That being said, you may explain how this answers the question. – PJProudhon Feb 21 '18 at 08:13