0
y = x?0:0x80

From googling the colon seems to be a ternary operator.

Dominic Bou-Samra
  • 13,072
  • 26
  • 92
  • 146

5 Answers5

6

That's right. (The correct name is the conditional operator. It is a ternary operator, in that it takes three operands, but it's commonly misnamed the ternary operator because it's the only JavaScript operator that does so.)

The code is roughly equivalent to this:

var y;
if (x) {
    y = 0;
}
else {
    y = 0x80;
}
LukeH
  • 242,140
  • 52
  • 350
  • 400
  • What exactly is the difference between a ternary operator and a conditional operator? Wikipedia calls it a ternary operation for JavaScript: http://en.wikipedia.org/wiki/Ternary_operation. – pimvdb Jul 25 '11 at 09:14
  • 2
    @pimvdb: The name "ternary operator" would apply to any operator that takes three operands. It just so happens that the only JavaScript operator that takes three operands is the conditional operator. – LukeH Jul 25 '11 at 09:16
1

It is a ternary operator. It assigns 0 to y if x is true, otherwise it assigns 0x80.

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
0

Yes, it's a ternary operation, assigning y the value of 0 if x is true or 0x80 otherwise.

andyb
  • 42,062
  • 11
  • 113
  • 146
0

It translates to:

if(x) then
    y=0
else
    y=0x80

But is much shorter.

Schroedingers Cat
  • 3,043
  • 1
  • 12
  • 33
0

Yes to all of the above, but it is also checking for the existence of x. If x doesn't exist or is null, y = 0x80.

davecoulter
  • 1,760
  • 10
  • 15