9

From the book Maintainable JavaScript it mentioned :

// Bad: Testing to see if an argument was passed
function doSomething(arg1, arg2, arg3, arg4){
if (arg4 != null){
    doSomethingElse();
  }
}

but I feel using != null here is quite effective, it filtered both the case of argument not passed or it is passed as null

any reason why the author suggest it is bad ?

j08691
  • 204,283
  • 31
  • 260
  • 272
vancewang
  • 5,120
  • 3
  • 15
  • 14
  • 4
    How should we know what the author was thinking? Ask the author! – Eric Lippert Jul 16 '13 at 14:16
  • I assume this advice is offered because it is useful to distinguish between `null` and no argument. `undefined` usually means "not yet given a value" while `null` usually means "deliberately given a value of nothing". – apsillers Jul 16 '13 at 14:16
  • The explanation is on the page before the one that has the code you posted. – j08691 Jul 16 '13 at 14:17
  • It's not at all bad, and is probably one of the most appropriate uses of `==`. But ultimately it depends on what you're trying to guard against. –  Jul 16 '13 at 14:19

4 Answers4

9

!= null is false for undefined, it basically means x !== null && x !== undefined. != does type coercion, and one of the effects of that is that it treats null and undefined as equivalent (null == undefined is true).

So unless you want that, some people recommend not using != with null (or undefined).

And you're quite right that the code defends against two (well actually three) possibilities:

  1. Nothing was passed for the argument at all.

  2. null was passed.

  3. undefined was passed.

But the comment says it's a bad way to test whether an argument was passed, and it is: Because it doesn't catch passing null or undefined. (Passing in null and not passing in anything are different cases.) That doesn't necessarily mean it's a bad way to check the argument for a specific function.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Sure. In javascript you can have null values and undefined values. And they're not the same. A fast and easy approach to check if the arg is valid would probably be:

if (arg4)

And this way you check arg4 is not null, not undefined, and not false. Please be careful with this last condition, you may inform a false value consciously here :-)

EDIT: T.J. Crowder is right here. It's easy to use this and will work in most cases, but be careful.

Bartserk
  • 675
  • 6
  • 18
  • Best answer, unless arg4 is a Boolean :) – EkoostikMartin Jul 16 '13 at 14:21
  • 6
    *"The best approach to check if the arg is valid would probably be"* No, that will also weed out `""`, `0`, `NaN`, and `false` (in addition to `null` and `undefined`). There are lots of times when that will bite you. It's a great check for optional *object* arguments, but other than those, it gets dicey. (Not my dv) – T.J. Crowder Jul 16 '13 at 14:22
0

I would highly recommend watching http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare - a brilliant presentation on this matter from the man who created it himself.

Tro
  • 897
  • 9
  • 32
-1

Because you can pass anything to a function, also null. If you would to the following:

var obj = new MyObject();
// ...
obj = null;
// ...
doSomething(arg1, arg2, arg3, obj);

You would have passed an argument but your checking would fail because the passed reference is null. Always use the following:

if(typeof arg4 != 'undefined')
    // ...
  • Why would the checking fail? Seems clear that the code intends to also guard against `null`. –  Jul 16 '13 at 14:18
  • To be fair, `obj = undefined` *should* be identical to omitting an argument. You should address the difference between `null` and `undefined`. – apsillers Jul 16 '13 at 14:19
  • @CrazyTrain The author of the original code example was trying to explain that *unless you want to guard against `null`*, don't check against `!= null`. Checking against `null` is offered (in this case) as a *defect* of the code, where the actual intent is only to test the number of defined arguments. – apsillers Jul 16 '13 at 14:20
  • @apsillers: So the author is saying don't exclude values that you want to include. Good advice. –  Jul 16 '13 at 14:25