DEV Community

Discussion on: Nested Conditional Operators

Collapse
 
itsasine profile image
ItsASine (Kayla)

I like multiple conditionals, but I make sure to keep some sort of nesting and breaking strategy to keep it readable. Innately, I can't think of a reason not to use nested other than readability, so if future you will still know what it does, go for it!

Most likely, I would have written that as

const validateInput = ({ field1, field2, field3 }) => (
    !field1 ? Promise.reject('Missing field1') : 
    !Array.isArray(field2) ? Promise.reject('field2 is not an array') :
    !isValidType(field3) ? Promise.reject('field3 is invalid') :
    Promise.resolve()
)

Whatever way works for you to see the ifs and elses without hunting for them.