🎓 A1Learner
🔍
0 of 54 lessons completed

Elixir Atoms

An atom is a name whose value is itself. Atoms often label results and choices, such as :ok for success.

Atoms are their own values

An atom prints with its colon under inspect. The same atom always equals itself.

IO.inspect(:ok)
IO.puts(:ok == :ok)
IO.puts(:ok == :error)

true, false and nil are atoms

The familiar values true, false and nil belong to the atom family. The is_atom check proves it for each one.

IO.puts(is_atom(true))
IO.puts(is_atom(false))
IO.puts(is_atom(nil))
IO.puts(is_atom(:yes))

Atoms differ from strings

An atom and a string can hold the same word. They are still different values of different types.

IO.puts(is_atom(:ok))
IO.puts(is_atom("ok"))
IO.puts(:ok == "ok")

Example

status = :ok
IO.inspect(status)
IO.puts(status == :ok)
IO.puts(is_atom(status))
IO.puts(is_atom(false))

Key points

  • Writing atoms: an atom starts with a colon, like :ok.
  • Self value: :ok is equal to :ok because an atom stands for itself.
  • Common labels: programs use atoms such as :ok and :error to mark outcomes.
  • Built-in atoms: true, false and nil are atoms in Elixir.
  • Type check: is_atom tells whether a value is an atom.

In this example, the variable status holds the atom :ok, and inspect prints it with its colon. The status equals :ok, it passes the is_atom check, and false passes that check too.

Note

text in double quotes is a string, not an atom, even when the words match.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which of these values is an atom?