Ternary conditional statement for else if

I can use the ternary conditional operator for the operator if {} else {}as follows: a ? x : yor question ? answer1 : answer2.

Can this format be used with a sentence else if? For instance. sort of:

a ? b ? x : y : z

... or is it just an excess?

+4
source share
1 answer

Both xand yin a ? x : yare full expressions, so you are allowed to insert any subexpressions into them while they produce the results of the correct type.

However, nesting conditional expressions quickly becomes unmanageable, so using parentheses is a very good idea:

let res = a ? (b ? x : y) : z

or

let res = a ? x : (b ? y : z)

or even

let res = a ? (b ? w : x) : (c ? y : z)
+6
source

Source: https://habr.com/ru/post/1653831/


All Articles