0

I would like to know the meaning of two symbols "?" and ":" in JavaScript. I think it is related to logical values, like (var ? false: true) but I am not sure about this.

Here goes an example of a function where this is used.

function KeyPadOpen(obj, opt) { 
    var recon = parseInt(document.getElementById('enc_porta').getAttribute("recon"));   
    var config = parseInt(document.getElementById('enc_porta').getAttribute("config"));
    var hidePad = (recon == 1 || config == 1);
    
    KeyPadClear(hidePad, true); 
    document.getElementById('enc_porta').setAttribute("config", (hidePad ? 0 : 1)); 
}

hidePad is an argument of a second function.

  • 1
    Another duplicate: [How do you use the ? : (conditional) operator in JavaScript?](https://stackoverflow.com/questions/6259982/how-do-you-use-the-conditional-operator-in-javascript) – Biffen Sep 21 '22 at 08:37
  • It's the [ternary operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator) - effectively a shorthand for an `if`/`return` statement – Rory McCrossan Sep 21 '22 at 08:37
  • It's ternary if operator: `condition ? 42 : 0`, same as `if(condition) { 42 } else { 0 }` (Simplified for easier understanding) – NNL993 Sep 21 '22 at 08:37
  • 3
    All - It's properly called [the **conditional** operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator). While it's true it's **a** ternary operator (an operator with three operands, just like a binary operator has two and a unary operator has one), and it's currently the only ternary operator in JavaScript, that could change. – T.J. Crowder Sep 21 '22 at 08:41
  • in your case gives 0 if true and 1 if false. So you just need to flipflop your assumption (var ? true : false ) – Roko C. Buljan Sep 21 '22 at 08:57

0 Answers0