2

I know that how simple conditional statements are interpreted, like

condition ? expr1 : expr2 

But I wanted to know how such a statement is interpreted?

function arc() {
    ...
    return da >= d3_svg_arcMax ? r0 ? "String1" : "String2" : r0 ? "String3" : "String4";
}

No need to explain this long expression. I just need to know what it means when multiple question marks and colons are used together like in this example.

Thank you

Saman
  • 439
  • 7
  • 16
  • 8
    _" I just need to know what it means when multiple question marks and colons are used together like in this example."_ -> Maintenance nightmare – James Thorpe Aug 10 '16 at 13:58
  • @JamesThorpe It would be better if they used `()` to wrap these. Yet still, an if-else would have worked a charm for readability. – somethinghere Aug 10 '16 at 13:58
  • Possible duplicate of [Operator precedence with Javascript Ternary operator](http://stackoverflow.com/questions/1788917/operator-precedence-with-javascript-ternary-operator) – gcampbell Aug 10 '16 at 14:02
  • @gcampbell should all colons and question marks be interpreted from right to left like the other operators? – Saman Aug 10 '16 at 14:08
  • 1
    This is already answered here very well http://stackoverflow.com/questions/7407273/why-is-the-conditional-operator-right-associative – vbuhlev Aug 10 '16 at 14:17

3 Answers3

4

Javascript is right-associative, so you 'resolve' the ternaries from right to left.

Taylor Daughtry
  • 249
  • 1
  • 9
  • 1
    How will be the parenthesis here: `da >= d3_svg_arcMax ? r0 ? "Z" : "M0" : r0 ? "M" : "M";` – Saman Aug 10 '16 at 15:19
  • 1
    Like @Saman, I'm also still wondering this. The code I'm seeing has two question marks before any colons, but all answers here don't give examples of this. Here is an example I found, that I'm confused by: https://pastebin.com/FuHJyHyS – Netside Feb 27 '20 at 01:10
  • 1
    @Netside this is why you should never ever do this. In the example you link, if `elem` is truthy and it's not an array return `steamrollArray(arr, [elem, ...flatArr])` if it is an array return `steamrollArray(arr.concat(elem), flatArr)` and if `elem` is not truthy return `flatArr`. Even if you know how ternary's work you *still* have to stop, take a minute, and map the control flow in your head. PITA – Jared Smith Mar 02 '20 at 16:31
4

First off,

Never do this. Ever. To add to Taylor's (correct) answer, if you can't resist the siren song, then grouping things using indentation is preferable (for some definition of 'preferable').

var foo = a ?
  b:
  c ?
    d:
    e;

Which makes it a little easier to see that foo will be b if a is truthy, d if c is truthy, or e otherwise.

Community
  • 1
  • 1
Jared Smith
  • 19,721
  • 5
  • 45
  • 83
  • 2
    Excellent point. You can also use parenthesis on the innermost conditions to make it take up less lines, but as you say, using multiple lines is the only way you will be able to read this semi-clearly. And a minifier will revert it anyway so it costs nothing. – Ed Bayiates Aug 10 '16 at 14:13
  • 1
    This doesn't exactly match the syntax he's asking about. His has multiple ? marks before any colons. – Netside Mar 02 '20 at 04:49
  • @Netside doesn't matter. My example could have easily been `foo = a ? b ? c : d : e;`. It just depends on which part of the first conditional you put the second conditional into. – Jared Smith Mar 02 '20 at 16:27
  • 1
    It does matter to the person asking. He may not understand it if it doesn't match what he's seeing and asking about -- not to sound rude or anything. Isn't var foo = a ? b : c ? d: e; different from foo = a ? b ? c : d : e;? – Netside Mar 04 '20 at 15:57
  • @Netside yes, and that's fair, but as I said this is mostly an addendum to the accepted answer. – Jared Smith Mar 04 '20 at 20:36
1

Here I found a good answer for my problem:

http://www.codeproject.com/Questions/1117764/Where-do-you-put-parentheses-in-the-following-java

Saman
  • 439
  • 7
  • 16