HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to ElixirVariablesData TypesOperatorsStringsListsConditions (if / else)Iteration (Enum)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 List & Enum
🔀 Condition
🧩 Function
Elixir›Variables

Variables

Value binding with = and the concept of immutability.

Simple enough for a 5-year-old to understand

Value binding

= binds a value to a name. In Elixir, this is called "matching/binding", not "assignment".

Example code
1
name = "Alex"
Assigns "Alex" to name.
VariableAssigns "Alex" to name.
2
age = 36
3
IO.puts(name)
Alex
Output / InputAlex
4
IO.puts(age)
36
Output / Input36
VariableOutput / Input
💡 Hint: Data is immutable in Elixir; you can rebind a name to a new value.

About this topic

In Elixir, the equals sign is not an assignment operator, but a matching operator. It tries to match the left side to the right side; if the left side is an unbound name, a value is bound to it. That's why it looks like it works as an assignment, but the underlying mechanism is different.

Data is immutable. Every operation where you think you are modifying a list actually produces a new list. However, names can be rebound: you can assign a new value to the same name. In Erlang even this is forbidden, but Elixir introduces flexibility in this regard.

The true power of the matching operator reveals itself in pattern matching. You can unpack parts of a tuple in a single line and verify that incoming data matches your expected format. This is the characteristic structure of Elixir code.

What you will learn

  • The meaning of the matching operator
  • Rebinding
  • Immutability
  • Pattern matching
  • The pin operator

Who is this for?

  • Beginners to Elixir
  • Those expecting assignment and encountering matching
  • Those seeing pattern matching for the first time

When to use?

Validating the structure of incoming data and extracting its parts is done with a single match expression; there's no need to write separate checks.

When you want to ensure that a value does not change, the pin operator is used; thus, accidental rebinding is prevented.

Common mistakes

Wrong

x = 1
2 = x

The equals sign performs a match; because the fixed value on the left side does not match the right side, the error "no match of right hand side value: 1" is raised. This is surprising to those expecting assignment, but it makes sense: the expression is an equation and if the equation is not satisfied, it throws an error.

Correct

x = 1
1 = x

Wrong

x = 1
if true do
  y = 5
end
IO.puts y

Names bound inside a block do not leak outside; the compiler issues a warning and an error occurs at runtime when used. Blocks in Elixir are expressions and produce values; to carry a value outside, you need to bind the result of the block to a name.

Correct

x = 1
y = if true do
  5
end
IO.puts y

Wrong

beklenen = 10
gelen = 20
beklenen = gelen
IO.puts beklenen

There is no error here, but the intent was likely expressed incorrectly: the name was rebound and no validation was performed. If you want to test that a value is equal to the expected one, you must use the pin operator; then a mismatched value will produce an error.

Correct

beklenen = 10
gelen = 20
^beklenen = gelen

Frequently asked questions

Won't memory fill up if values are immutable?

It won't, because the virtual machine shares immutable parts: when you create a new list, the old elements are not copied, the same memory is shared. Immutability is thus both safe and efficient.

When is the pin operator needed?

When you want to use the current value of a name within a pattern. Without the operator, the name is rebound and the match always succeeds; with the operator, the value is compared and if it doesn't match, it errors out.

How do I get rid of the unused variable warning?

You put an underscore at the beginning of the name. This tells the compiler that you are intentionally not using the value. In pattern matching, writing just an underscore for parts you are not interested in is also common.

Summary

The equals sign is a matching operator.

A non-matching match throws a runtime error.

Names can be rebound, data is immutable.

The binding inside the block does not leak outside.

The pin operator compares against the current value.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code