views:

471

answers:

6

I've seen this format used in JavaScript code, but can't find a good source for the meaning.

Edit for a follow-up:

Thanks for all the quick answers! I figured it was something like that. Now, for bonus points:

can you use (var1 ? var2)

to do the same thing as

    if (var1) {
        var2
    }

?

+6  A: 

Its a conditional operator.

It is

if var1 then var2 else var3

Read more here

Conditional Operator

The conditional operator is the only JavaScript operator that takes three operands. This operator is frequently used as a shortcut for the if statement.

rahul
May I know the reason for the down vote?
rahul
-1. This really is taking the wrong tack. You would not want the value expressions modifying anything, you just want the value of the overall expression to be either the value on the true side or the value on the false side, that is the purpose of this operator.
AnthonyWJones
@phoenix Maybe your example does not show exactly a useful usage of the conditional operator. It can be simplified to `var xCon = num == 1;`
CMS
var whatever = num < 5 ? 'less than 5' : '5 or bigger';
Maurice
+2  A: 
if(var1) {
    var2;
else {
    var3;
}
Koraktor
+1  A: 

This seems to be sort of a ternary operation. Short form of an if else operation, so to say. check here for details...

KB22
+2  A: 

The expression var1 ? var2 : var3 returns the value of var2 if var1 is considered to have a value equivalent to true else it returns teh value of var3.

Note this is not quite the same as:-

if (var1)
   varX = var2
else
   varX = var3

Since the above construct can not itself appear as part of a larger expression.

In ternery expression, as ? : is known, one should avoid allowing the component expressions to have side effects other than perhaps the side-effects of ++ or -- operators. For example this isn't a good idea:-

varX = var1 ? doSomethingSignificant() : doSomethingElseSignificant();

In this case it would be better to use the if else construct. On the hand:-

varX = var1 ? calcSomething(var2) : someOtherCalc(var2);

this is acceptable assuming the called functions don't themselves modify the program state significantly.

Edit:

I think I need to re-enforce this point. Do not use the ternary operator as means to short cut on if statements. The two have different purposes. If your code is full of ? : that should be if else it will be difficult to read. We expect logical flow to appear in if statements. We expect ? : when there is a simple logical component to an expression. Note expressions do not modify things only the results of them when assigned should modify things.

AnthonyWJones
I think a simpler way of stating that last part is: The ternary operator's job is **to be equal to one value or another** based on a condition, while if statements in Javascript don't yield any value at all — their job is to **execute code**.
Chuck
@Chuck: That is simpler however there is no reason why an expression shouldn't __execute code__ in a Ternary operator, its what that code does (or more to the point does not do) thats important. Ideally the program state should not change as a result of the expression being evaluated.
AnthonyWJones
+17  A: 

It's known as a ternary (because it has three operands) conditional (because it's an if/else/then) operator.

It is evaluated to a value, so you would usually use it to assign a value, such as:

var result = condition ? value1 : value2;

Which is equivalent to:

var result;
if (condition == true) {
  result = value1;
} else {
  result = value2;
}

An example:

var message = "Length is " + len + " " + (len==1 ? "foot" : "feet");

http://en.wikipedia.org/wiki/Ternary_operation

Drew Noakes
I think your first == is a typo.
eyelidlessness
Also the two bits of code are not equivalent, as the second should be preceded by "var result;"
eyelidlessness
@eyelidlessness -- apparently I shouldn't be allowed to code before 7.30AM. I made the same == vs = mistake on SO yesterday. Thanks for pointing these out.
Drew Noakes
Never EVER program before you've finished your first cup of coffee. :)
Chris Sobolewski
+1  A: 

As an addendum for the first question, you can alternatively use

  var result = (condition) && var1 || var2;

and obtain the same result

For the second question, in C the following works too :

  (condition) && someinstruction;

but that does not seem to work in javascript (at least with my version of firefox).

ffx
Yes it should work. `(condition) ` should evaluate `someinstruction` only when `condition` is of truthy value.
kangax