Learn about different kinds of Elixir operators and their usage.

An operator is used to manipulate individual data items and return a result. These items are called operands or arguments. Operators are represented by special characters or keywords. For example, the multiplication operator is represented by an asterisk (*). Elixir has a very rich set of operators. Here’s a list of all operators that Elixir is capable of parsing, ordered from higher to lower precedence, alongside their associativity:

  • Comparison operators
  • Boolean operators
  • Relaxed boolean operators
  • Arithmetic operators

Let’s cover them in detail.

Comparison operators

  • a===b shows the strict equality (so 1 === 1.0 is false).

  • a!==b shows strict inequality (so 1 !== 1.0 is true).

  • a==b shows value equality (so 1 == 1.0 is true).

  • a!=b shows value inequality (so 1 != 1.0 is false).

  • a>b shows the value is greater than the other (so 2>1 is true).

  • a>=b shows the value is greater than or equal to the other (so 2>=1 is true).

  • a<b shows the value is less than the other (so 1<2 is true).

  • a<=b shows the value is less than or equal to the other (so 1<=2 is true).

The ordering comparisons in Elixir are less strict than in many languages, as we can compare values of different types. If the types are the same or are compatible (for example, 3 > 2 or 3.0 < 5), the comparison uses natural ordering. Otherwise comparison is based on type according to this rule: number < atom < reference < function < port < PID < tuple < map < list < binary.

Boolean operators

These operators expect true or false as their argument.

  • a or b

  • a and b

  • not a

Relaxed boolean operators

These operators take arguments of any type. Any value apart from nil or false is interpreted as true. Below are relaxed boolean operators supported by Elixir:

  • a||b gives true if a is true. Otherwise, it gives b.
  • a && b gives false if a is false. Otherwise, it gives b.
  • !a gives false if a is true. Otherwise, it gives true.

Arithmetic operators

Below are arithmetic operators supported by Elixir:

  • + is an add operator (so 1+2 is 3).

  • - is a subtract operator (so 2-1 is 1).

  • * is a multiplication operator (so 2*1 is 2).

  • / is a division operator that gives result in float (so 1/2 is 0.5).

  • div returns a quotient on division (so div(2,1) is 2).

  • rem returns remainder on division (so rem(5,2) is 1). Integer division yields a floating-point result. We use div(a,b) to get an integer. The remainder operator is rem. It’s called as a function, like (rem(11, 3) => 2).

Get hands-on with 1200+ tech skills courses.