🎓 A1Learner
🔍
0 of 54 lessons completed

Elixir Comments

A comment is a note inside code that Elixir ignores when the program runs. Comments help people who read the code later.

A comment on its own line is ignored

The first line is a note for the reader. Only the second line runs.

# This line is a note
IO.puts("Hello")

A comment can follow a statement

The statement runs first on this line. The note after the # symbol is ignored.

IO.puts("Visible") # This note is ignored

Commented-out code does not run

The first line looks like a statement, but the # symbol turns it into a note. Only the second line runs.

# IO.puts("Hidden")
IO.puts("Shown")

Example

# A small program with notes
IO.puts("First line")
# The next line prints the second message
IO.puts("Second line")

Key points

  • Comment: a note inside code that Elixir ignores.
  • Start: a comment starts with the # symbol.
  • End: a comment runs to the end of its line.
  • Use a comment to explain what the next line does.
  • Placement: a comment can sit on its own line or after a statement on the same line.

In this example, the first line is a comment, so Elixir skips it. The next statement prints First line. Another comment is skipped, and the last statement prints Second line.

Note

Elixir has no block comments, so a long note uses one # line for each line of text.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which symbol starts a comment in Elixir?