2

Possible Duplicate:
Question mark in JavaScript

I've seen this around a few times but I never knew what it meant. What is the name of it and how does it work?

Here is the example I saw:

input.checked = input.type == "radio" ? true : false;
Community
  • 1
  • 1
0x499602D2
  • 87,005
  • 36
  • 149
  • 233

4 Answers4

6

That example has an extra = in it, I think you meant:

input.checked = input.type == "radio" ? true : false;

(It's fixed now.)

It assigns true to input.checked if input.type == "radio", or false if it doesn't.

That

expression ? trueResult : falseResult

...is called the conditional operator (or sometimes, the "ternary" operator — technically, it's just a ternary operator, e.g., an operator that takes three operands). More in Section 11.12 in the spec.

In this case, there's absolutely no point in using the conditional operator, because the result of the equivalence expression is true or false anyway, so it could be written just:

input.checked = input.type == "radio";

...but there are lots of places where the conditional operator is useful. For instance, suppose you wanted to assign 1 or 2 to x depending on whether y was 42:

x = y == 42 ? 1 : 2;

You can think of the ? as asking a yes-or-no question, with what follows it being the "yes" answer, and what follows the : being the "no" answer.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
  • Does the "ternary" operator require a falseResult value?? Can I leave it out if I want? – 0x499602D2 Aug 15 '11 at 18:20
  • @David: The value of the condition expression (the expression before the `?`) will be coerced to a boolean if it isn't a boolean based on the usual JavaScript rules (just like the expression you use in an `if` statement). I'm not sure what you mean by "can you leave it out." If you're assigning `true` or `false` as in your question, then as I said, there's no need for the conditional operator -- just assign the result directly to `checked`. But if you want to assign something other than `true` or `false`, the conditional operator is a handy way to do that in some situations. – T.J. Crowder Aug 15 '11 at 18:24
  • @David: Oh, sorry, I see what you mean. Yes, the false part is required when using the conditional operator. You have to have something between the `:` and the end of the expression. Otherwise use an `if` or JavaScript's [curiously powerful `||` and `&&` operators](http://blog.niftysnippets.org/2008/02/javascripts-curiously-powerful-or.html). – T.J. Crowder Aug 15 '11 at 18:25
3

Maybe it's easier to understand like this:

input.checked = (input.type == "radio")? true : false;

It's basically an if else. If the expression is true then input.checked will be set to the first value, else the second value.

[edit]

As a note, in JavaScript, you should always use '===' instead of '==' when evaluating strings to check type equality as well.

Snps
  • 13,516
  • 6
  • 49
  • 72
0

This is a ternary expression. It is a shorthand for:

if (input.type == "radio") {
    input.checked = true;
} else {
    input.checked = false;
}

However, there is redundancy in this particular example. It could have been simply written as:

input.checked = input.type == "radio";
Ates Goral
  • 126,894
  • 24
  • 129
  • 188
0

Maybe the first == is =?

It looks like ternary operator. (short else-if structure)

Larry Cinnabar
  • 10,538
  • 15
  • 53
  • 88
  • Does the ternary operator require a false result value?? Can I leave it out if I want?? – 0x499602D2 Aug 15 '11 at 18:23
  • In javascript is required. You can't do something like `a = ( x == 3) ? 5;` But you can do in such a way: `a = ( x==3 ) ? 5 : a`. It means in a false result case, `a` will be equals to `a` – Larry Cinnabar Aug 15 '11 at 18:25