20

I was taking a look at some node.js code earlier, and I noticed that the guy who wrote it seemed to favour the following syntax:

var fn = function (param) {
    var paramWithDefault = null == param ? 'Default Value' : param;
}

Over what I consider to be the more concise:

var fn = function (param) {
    var paramWithDefault = param || 'Default Value';
}

I was wondering if the second form is actually more socially acceptable JavaScript syntax, I've seen it out in the wild more times than the ternary operator for this purpose.

I note that in the first example he's using the double equals (not the triple equals) which means it will count "undefined" as null, which would reduce one impact that I could think of. However, I've read in numerous places that == is a rather evil operator in JavaScript (JSLint is very much against it, IIRC).

1
  • 2
    Commenters: comments are meant for seeking clarification, not for extended discussion. If you have a solution, leave an answer. If your solution is already posted, please upvote it. If you'd like to discuss this question with others, please use chat. See the FAQ for more information.
    – user8
    Commented Jun 8, 2011 at 23:15

2 Answers 2

18

Because this code would evaluate to 'Default Value' everytime you passed in 0, "", false, or some other falsy value.

function fn(param) {
  var paramWithDefault = param || 'Default Value';
  return paramWithDefault;
}

It might not bite you on how you use this particular function, but it is a bad pattern to avoid when you do care about passing in things like empty strings or 0 or a boolean.

4
  • you should only use a null coalesce on an object, and if an object is defined, then this won't work. With maybe the exception of the empty string.
    – Malfist
    Commented Jun 8, 2011 at 20:55
  • 4
    The zero comparison is a good point, that could be quite unexpected.
    – Ed James
    Commented Jun 8, 2011 at 21:08
  • 1
    +1 - this problem is precisely why Python (eventually) added the "x if y else z" syntax. Those semantics for logical operators are fairly common, and the same common errors tend to crop up whenever idioms rely on them to do the work of conditional selection operators.
    – user8709
    Commented Jun 8, 2011 at 22:00
  • just don't forget to put your constructs in parenthesis, if you use them together with string concatenation var txt = 'Hello, ' + (user_name||'User') + '!'; will work, but without parenthesizes - you will get undefined. jsfiddle.net/4mFAB/1
    – c69
    Commented Dec 10, 2011 at 23:21
8

What you really need is a null coalesce operator. But seeing that javascript doesn't really have one, programmers typically use '||' to stand in for it.

However, both are perfectly reasonable. To those that don't understand what a null coalesce operator is, the ternary operator is probably more likely to be understood.

3
  • Another related operator is the Icon operator spelled IIRC "else". This recognises a special "fail" result from the first argument, and uses the second argument in that case as an alternative. I kinda wish Pythons "x if y else z" were implemented using two separate operators - an "if" binary assertion operator and an Icon-like "else" operator - with those two operators being usable independently. However, Icon didn't support that style, instead doing something bizarre with relative operators.
    – user8709
    Commented Jun 8, 2011 at 21:56
  • @Steve314: Python does have something you wanted: a separate else operator [false-part, true-part] with a separate if operator [..][bool(condition)] combined into [false-part, true-part][bool(condition)]. If you want lazy behavior, you can simply lambda the true- and false-part.
    – Lie Ryan
    Commented Jun 9, 2011 at 21:28
  • C#'s version on MSDN
    – ruffin
    Commented Apr 4, 2013 at 14:06

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.