While using && to actually return the latter is indeed a cool trick, it's probably confusing for beginners. And in my opinion the ternary operator comes in more handy in this case.
Here's a simple approach which uses the ternary operator and should also be pretty easy to understand for beginners:
While using
&&
to actually return the latter is indeed a cool trick, it's probably confusing for beginners. And in my opinion the ternary operator comes in more handy in this case.Here's a simple approach which uses the ternary operator and should also be pretty easy to understand for beginners:
That works, because
is basically the same as
(Think of
?
and:
as 'then' and 'else')And since we can leave away the curly braces in this case (since it's just one expression), we can simply use this arrow function:
So this was a brief explanation (hopefully someone understands what I'm saying) of the ternary operator, see MDN to learn more