0

This function is supposed to take two parameters, however there are characters included that I do not understand what they mean. What is the value of "?". What are the two parameters in this function, I know panel.id is one of them . any link to a library that explain them well ? thank you

setPanelType(panel.id, ((encType) ? PANEL_ST_ENC : PANEL_NORMAL))

The duplicate question posted here might be explaining what the "?" operator is. However I was not sure if it is used differently in a function parameter call. This question is not a duplicate of any.

Mozein
  • 787
  • 5
  • 19
  • 33
  • `... ? ... : ...` is called the [Conditional (ternary) Operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator) – Andreas Jan 21 '15 at 18:50
  • 2
    I assume this code is in Java, or in JavaScript. It would be best to only tag the language it's actually in. (Java != JavaScript) – T.J. Crowder Jan 21 '15 at 18:56
  • Side note: All of the parentheses in that code other than the outermost ones are completely unnecessary (Java or JavaScript, or just about any other language syntactically derived from B). E.g., `setPanelType(panel.id, encType ? PANEL_ST_ENC : PANEL_NORMAL)` would be fine. – T.J. Crowder Jan 21 '15 at 18:57
  • The project I am working on has both javascript and java code implemented. I to be honest was not sure this code lands to which language. – Mozein Jan 22 '15 at 16:20

3 Answers3

3

You've run in to something called the "conditional operator"*. It's basically a short way of writing an if-statement.

For example:

String var;
var = 1 > 0 ? "It's bigger than 0" : "It's 0 or smaller";

Is the same as:

String var;
if(1 > 0){
    var = "It's bigger than 0";
}else{
    var = "It's 0 or smaller";
}

* It's also sometimes called the "ternary" operator, but that's not quite correct. It's a "ternary operator" (an operator that accepts three operands, just like the multiplication operator * is a binary operator because it accepts two operands), but in theory there could be others. In fact, I think it's the only ternary operator in Java or JavaScript (at least for now).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Bono
  • 4,757
  • 6
  • 48
  • 77
0

This syntax is shorthand for a conditional action in Javascript.

(condition) ? (true action) : (false action)

Related: JavaScript ternary operator example with functions

Community
  • 1
  • 1
Blake Frederick
  • 1,510
  • 20
  • 31
0

The '?' means Ternary Operator as said above, encType is a boolean variable.

setPanelType(panel.id, ((encType) ? PANEL_ST_ENC : PANEL_NORMAL))

is equals to:

if (encType)
   setPanelType(panel.id, PANEL_ST_ENC))
else 
  setPanelType(panel.id, PANEL_NORMAL)) 
Johnny Willer
  • 3,717
  • 3
  • 27
  • 51