Elixir Syntax

From Elixir Wiki
Jump to navigation Jump to search


Elixir is a dynamic, functional programming language built on top of the Erlang virtual machine (BEAM). It provides a concise and expressive syntax that allows developers to write clean and maintainable code. This article aims to provide an overview of the Elixir syntax, covering its key features and conventions.

Data types[edit]


An atom is a constant whose value is its own name. It is used to represent static values, such as status codes or names. Atoms are declared by writing a lowercase word or a word enclosed in single quotes:


'Hello, World!'


Elixir supports both integers and floating-point numbers. Integers can be written in decimal, hexadecimal, octal, and binary representations. Floating-point numbers can be written using the decimal or the scientific notation:

Example: 10 0xFF 0o777 0b1010 3.14 12.5e-3


Strings in Elixir are delimited by double quotes. They can contain escape sequences and interpolations:

Example: "Hello, World!" "Today is #{Date.utc_today()}."


A tuple is an ordered collection of elements, enclosed in curly braces. Tuples can hold any combination of data types:

Example: {:ok, 42} {"John", 25}


A list is a dynamic collection of elements, enclosed in square brackets. Lists can hold any combination of data types and are primarily used for pattern matching and recursion:

Example: [1, 2, 3] [true, "Hello", :ok]


A map is a collection of key-value pairs. Keys can be any data type, and values can be any Erlang term. Maps are defined using a literal syntax or the %{} syntax:

Example: %{name: "John", age: 25} %{:ok => "Success", :error => "Failure"}

Anonymous Functions[edit]

Anonymous functions, also known as lambda functions, allow you to create function literals. They are declared using the fn keyword followed by argument names and the function body:

Example: fn x -> x * 2 end fn a, b -> a + b end

Control flow[edit]

Pattern Matching[edit]

Pattern matching is a powerful feature in Elixir that allows for complex data manipulation and conditional branching. It is used extensively in function clauses and case statements:

Example: {status, message} = {:ok, "Success"} case response do

 {:ok, result} -> IO.puts("Result: #{result}")
 {:error, reason} -> IO.puts("Error: #{reason}")


Conditional Expressions[edit]

Elixir provides if, unless, and case expressions for control flow based on conditions:

Example: if condition do

 # do something


 # do something else


unless condition do

 # do something


 # do something else


case value do

 pattern1 -> # do something
 pattern2 -> # do something else
 _ -> # default case



This article provided an introduction to the Elixir syntax, covering the key data types, control flow constructs, and conventions. Understanding the Elixir syntax is essential for writing clean, efficient, and maintainable code in the Elixir programming language. For more in-depth information, refer to the articles on specific topics available on this wiki.