Logical Expressions
The boolean type in Elixir can be either true or false. You can use
logical operators like and, or, and not to manipulate these boolean
values:
iex> true and true
true
iex> true or false
true
iex> not true
false
Elixir’s and, or, and not operators strictly work with boolean values. But
there’s more! Elixir also provides && (and), || (or), and ! (not)
operators that can handle truthy and falsy values, giving them a bit of
flexibility. In Elixir, every value is considered truthy except for false
and nil, which are falsy.
To clarify:
-
&&(and) returns the first falsy value or the last value if all are truthy. -
||(or) returns the first truthy value or the last value if all are falsy. -
!(not) returnsfalseif its argument is truthy andtrueif it’s falsy.
Let’s consider a few examples:
iex> true && :hello
:hello
iex> false || "world"
"world"
iex> !nil
true
In the first example, :hello is returned because true && :hello evaluates to
the last truthy value, which is :hello. In the second example, "world" is
returned because false || "world" evaluates to the first truthy value, which
is "world". In the final example, !nil gives true because nil is a
falsy value and ! flips it to true.