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 ?

4
  • 4
    How should we know what the author was thinking? Ask the author! Commented Jul 16, 2013 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". Commented Jul 16, 2013 at 14:16
  • The explanation is on the page before the one that has the code you posted. Commented Jul 16, 2013 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. Commented Jul 16, 2013 at 14:19

4 Answers 4

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.

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

Comments

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.

2 Comments

Best answer, unless arg4 is a Boolean :)
"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)
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.

Comments

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

4 Comments

Why would the checking fail? Seems clear that the code intends to also guard against null.
To be fair, obj = undefined should be identical to omitting an argument. You should address the difference between null and undefined.
@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: So the author is saying don't exclude values that you want to include. Good advice.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.