Define a variable in Elixir with the equals = operator.

The = operator is also used for pattern matching.

# Valid variable assignments

stuff = "stuff"
valid_variable = "Hi im valid"
_me_too = "Im also valid"
me_good? = true

# Valid variable assignments but discouraged

dontDoThis = "Hi from Java"

# Invalid variable assignments

BooHoo = "Im invalid and will cause an error" # -> ** (MatchError) no match of right hand side value: "Im invalid and will cause an error"

# To perform a pattern match rather than an assignment 
# Prefix the left hand side with the ^ operator.  
^stuff = "things" # -> ** (MatchError) no match of right hand side value: "things"


  • Variables can start with a lowercase [a-z] or an underscore _
  • Variables can contain upper/lower case [a-zA-Z] and the _ underscore characters.
  • snake_case is the formatting convention used for variable naming.
  • Variables can end with a question mark ? or an exclaimation mark !
  • Variables that have been previously assigned can be reassigned in Elixir. This differs from Erlang.
  • If a variable is reassigned the value in the memory address is not mutated. The variable is assigned a new memory address.
# elixir