View Source Hello World!

Here's how you write a Hello World script in Elixir.

IO.inspect("Hello World!")
"Hello World!"

Variable Assignment

Assigning a value to a variable is super simple in Elixir. You just write my_var = and whatever is on the right side of the = sign gets assigned to the variable. Here is how to assign all the different basic types to variables:

name = "Peter" # String
age = 32 # Integer
age_hex = 0x20 # Integer in Hex notation
height = 190.47 # Float
height_sci = 1.9047e2 # Float in scientific notation
adult? = true # Boolean
status = :active # Atom
address = nil # The 'None/null/Nil' value

IO.inspect([name, age, age_hex, height, height_sci, adult?, status, address])
["Peter", 32, 32, 190.47, 190.47, true, :active, nil]

Variables are immutable but can be reassigned. This means that the memory holding the old value will not be overwritten; instead, new memory is allocated to hold the new value while the old value remains until garbage collected. Even if you modify a variable, it is not updated in place but copied to a new chunk of memory. This might seem wasteful, but it prevents different processes from modifying the same variable.

age = 32
age = 21
IO.inspect(age)
21

You can ignore assignments by prefixing the variable name with an underscore:

_ = 30
_result = 30 + 32

String Interpolation and Concatenation

You can interpolate and concatenate strings like this:

# Interpolate a string with values
name = "Peter"
age = 32
IO.inspect("My name is #{name} and I am #{age} years old")

# Concatenate two strings
IO.inspect("But my real name is " <> "Batman 🦇")
"My name is Peter and I am 32 years old"
"But my real name is Batman 🦇"