Want to learn?
Elixir Course Online | Prograils - Software Development Company
Booleans
Representation
There are two boolean values supported by Elixir: true
and false
. There is, however, one more value that in the context of logical operations acts as false. This value is nil
. Any value that is not nil
or false
will be treated as true
.
You can check if given value is boolean with is_boolean/1
function:
iex> is_boolean 1
false
iex> is_boolean true
true
Logical operators
Boolean operators
The following operator requires the first argument to be either true
or false
.
a or b -> if the first argument is true then true, otherwise b
a and b -> if the first argument is true then b, otherwise false
not a -> if the argument is true then false, otherwise true
Relaxed boolean operators
Unlike previously introduced operators, relaxed boolean operators work with arguments of any type.
a || b -> if the first argument is true then a, otherwise b
a && b -> if the first argument is true then b, otherwise a
!a -> if the argument is true then false, otherwise true