User ternary operator for 3 possible outcomes

javascript, jquery, ternary-operator

Solution

statusFlag: $('#statusFlag').val() == 'true' ? true : 
            $('#statusFlag').val() == '' ? 'empty' : false

You can just add multiple conditions like this:

a ? b : (c ? d : e)

`()` aren't necessary, but in my opinion, they improve readability when you write it without linebreaks.

This means:

if(a){
    b
}else{
    if(c){
        d
    }else{
        e
    }
}

So, basically, you're nesting another ternary operator in the `else` clause of the previous one. You can go as far as you'd like with this:

a ? b :   // if(a) then `b`
c ? d :   // else, if(c) then `d`
e ? f :   // else, if(e) then `f`
g ? h :   // else, if(g) then `h`
i ? j : k // else, if(i) then `j`, else `k`

Problem

I want to write a ternary operator based on the following; ``` var statusJSON = { '- Select -': '', 'Active': true, 'Inactive': false }; ``` Currently I have ``` statusFlag: $('#statusFlag').val() == 'true' ? true : false ``` This works fine for `true` and `false` values, but does not handle the 3rd condition (i.e. empty `""`) How do I handle the same ?

Original source