3 Basic operatorsΒΆ

{% include toc.html %}

In the previous chapter, we saw Elixir provides +, -, *, / as arithmetic operators, plus the functions div/2 and rem/2 for integer division and remainder.

Elixir also provides ++ and -- to manipulate lists:

iex> [1,2,3] ++ [4,5,6]
iex> [1,2,3] -- [2]

String concatenation is done with <>:

iex> "foo" <> "bar"

Elixir also provides three boolean operators: or, and and not. These operators are strict in the sense that they expect a boolean (true or false) as their first argument:

iex> true and true
iex> false or is_atom(:example)

Providing a non-boolean will raise an exception:

iex> 1 and true
** (ArgumentError) argument error

or and and are short-circuit operators. They only execute the right side if the left side is not enough to determine the result:

iex> false and error("This error will never be raised")

iex> true or error("This error will never be raised")

Note: If you are an Erlang developer, ``and`` and ``or`` in Elixir
actually map to the ``andalso`` and ``orelse`` operators in Erlang.

Besides these boolean operators, Elixir also provides ||, && and ! which accept arguments of any type. For these operators, all values except false and nil will evaluate to true:

# or
iex> 1 || true
iex> false || 11

# and
iex> nil && 13
iex> true && 17

# !
iex> !true
iex> !1
iex> !nil

As a rule of thumb, use and, or and not when you are expecting booleans. If any of the arguments are non-boolean, use &&, || and !.

Elixir also provides ==, !=, ===, !==, <=, >=, < and > as comparison operators:

iex> 1 == 1
iex> 1 != 2
iex> 1 < 2

The difference between == and === is that the latter is more strict when comparing integers and floats:

iex> 1 == 1.0
iex> 1 === 1.0

In Elixir, we can compare two different data types:

iex> 1 < :atom

The reason we can compare different data types is pragmatism. Sorting algorithms don’t need to worry about different data types in order to sort. The overall sorting order is defined below:

number < atom < reference < functions < port < pid < tuple < maps < list < bitstring

You don’t actually need to memorize this ordering, but it is important just to know an order exists.

Well, that is it for the introduction. In the next chapter, we are going to discuss some basic functions, data type conversions and a bit of control-flow.