Righto, so I have come across this in a lot of code that I have read and I understand the functional role of the conditional operator in Javascript; what I do not understand is when one should use it.
I have read the ECMAScript Specification 5.1 on it, but it doesn't help me a great deal in deciding when to impliment it.
For example, I can quite easily see the benefit of replacing the code:
if (isTrue == true) {
doSomething();
}
else {
doSomethingElse();
}
With next:
isTrue ? doSomething() : doSomethingElse()
Where I fall down is when either the code inside the if... else... statement is lengthy or the conditional operator is nested inside (an)other conditional operator(s). The obvious candidate for deciding when to use which version of the conditional statement is "does replacing it with the conditional operator make the code easier to follow?". Unfortunately I am rather inexperienced with writing code and I am unsure of when this is and I'm biased by understanding my own code well, no matter how poorly written it may be.
Can anybody with significantly more experience than me give me a slightly less subjective test for when you use it, so that I can attempt to follow suit?