4
In my project I constantly come across this kind of conditional expression:
if(cliente && cliente.id) {
return cliente.id;
} else {
return '';
}
That is to say, "if there is a client and this client has an id then return the id value, otherwise return an empty string". This expression is useful for two reasons;
1- Avoid exceptions of the type Cannot read property of undefined/null
2- Do not return the literal undefined
to the interface
Is there any way to reduce this expression?
Observing: Zero is not part of the set of ids possible.
Excellent answer, +1! I like to see detailed and explanatory answers like this one; I already learned one more thing from Javascript today, so! o/
– Rui Pimentel