if and unless

These two constructs can be used with the following syntax:

if <expression> do  # expression was truthyelse  # expression was falsyendunless <expression> do  # expression was falsyelse  # expression was truthyend

As with the def construct, they can be inlined. For if, you'd do this:

if <expression>, do: # expression was truthy, else: # expression was falsy 

For both constructs, the else clause is optional. They will return nil if the main clause doesn't match and no else clause was provided.

Get Mastering Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.