Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

share|improve this question
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 Jun 8 '11 at 23:15

2 Answers 2

up vote 14 down vote accepted

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.

share|improve this answer
    
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 Jun 8 '11 at 20:55
4  
The zero comparison is a good point, that could be quite unexpected. –  Ed Woodcock Jun 8 '11 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. –  Steve314 Jun 8 '11 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 Dec 10 '11 at 23:21

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.

share|improve this answer
    
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. –  Steve314 Jun 8 '11 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 Jun 9 '11 at 21:28
    
C#'s version on MSDN –  ruffin Apr 4 '13 at 14:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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