Use short-circuits conditionals
If you want to execute a function when a condition matches, then use short-circuit to shorten the code:
if (formValid) {
submitForm();
}
formValid && submitForm();
The logical OR operator `||`
is used in a simliar way:
!formValid || submitForm();
In the modern web frameworks, we can use the similar syntax to render a given component when a condition satisfies.
The following sample code gives you the idea of
conditional rendering in
React:
{
unreadMessages.length > 0 && <div>You have {unreadMessages.length} unread messages.</div>;
}