Getting Started with Elixir - Variables and Operators

In this section we're going to cover how variables and operators are used in Elixir.

This may not be very exciting to more seasoned programmers but since we’re building Phoenix Applications in an Elixir ecosystem, understanding basic syntax is important.

Before getting started, install Elixir for your platform and verify that you can run Elixir’s Interactive Shell (IEx).

Versions

  • Elixir 1.10.0

Variables

Variables are assigned without any keywords:

iex> x = 5
5

iex> x
5

= is an operator used to “bind” values to a variable. We’ll cover more operators in a bit.

Results of an expression can be bound to a variable:

iex> x = 1 + 1
2

Variables are used throughout an Elixir program and because they’re black boxes, it’s best to use variable names that describe what’s in the box.

They also follow the “snake_case” convention in Elixir:

dog
dog_name
maybe_feed_dog?
dogName # works but doesn't follow convention

Basic Operators

Next, let's look at some of the basic operators.

String Concatenation

Concatenating strings in Elixir uses a unique operator: <>

iex> "Hello " <> "world!"
"Hello world!"

String Interpolation

String interpolation uses the syntax: #{}

iex> dog = "Spike"
"Spike"

iex> "Good boy #{dog}."
"Good boy Spike."

Arithmetic

Arithmetic operators are: +, -, *, /

iex> 1 + 1
2

iex> 2 - 1
1

iex> 2 * 2
4

iex> 4 / 2
2.0
💡
Note: / will always return a float.

Elixir also provides functions for Integer Division:

iex> div(4, 2)
2

And Remainder:

iex> rem(93, 7)
2

Booleans

Boolean operators include ||, && and ! which accept arguments of any type. All values except false and nil will evaluate to true.

|| - or

iex> false || "one"
"one"

iex> "one" || false
"one"

&& - and

iex> "one" && false
false

iex> "one" && "two"
"two"

! - negation

iex> !true
false

iex> !nil
true

There are three additional boolean operators: and, or and not. The difference is that they expect the first argument to evaluate to a boolean:

iex> "one" and true
** (BadBooleanError) expected a boolean on left-side of "and", got: "one"

iex> true and "one"
"one"

iex> false or "one"
"one"

iex> not true
false

From the docs:

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 !.

Comparison Operators:

All the usual suspects here: ==, !=, ===, !==, <=, >=, <, and >.

iex> 5 == 5
true

iex> 5 != 1
true

iex> 1 < 5
true

The difference between == and === in Elixir is strict comparison between integers and floats:

iex> 5 == 5.0
true

iex> 5 === 5.0
false

In Elixir you can also compare different data types:

iex> 5 < :atom
true

From the docs:

💡
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 < function < port < pid < tuple < map < list < bitstring

List Operators

When working with different lists we also have ++ and --

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

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

Wrapping Up

Here we briefly covered the basics of the Elixir language including a few conventions and nuances with basic operators.

-- Troy

Twitter: @tmartin8080  | ElixirForum

Subscribe to ReadReplica

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe