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

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:

= 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:

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:

Basic Operators

String Concatenation

Concatenating strings in Elixir uses a unique operator: <>

String Interpolation

String interpolation uses the syntax: #{}

Arithmetic

Arithmetic operators are:  +, -, *, /  

Note: / will always return a float.

Elixir also provides functions for Integer Division:

Booleans

Boolean operators include  ||, && and ! which accept arguments of any type. All values except false and nil will evaluate to 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:

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

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

In Elixir you can also compare different data types:

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

Wrapping Up

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

Armed with this knowledge, we'll jump into Elixir pattern matching in the next section.

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

Troy Martin
Senior Engineer working with Elixir and JS.
Keep in the Loop

Subscribe for Updates

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.