On Elixir Pt. 3

Jan 3, 2025

Elixir is a dynamic programming language. Assign the value to a variable, and you’re good to go.

Go ahead and create a new file named var.ex and write the following code in it:

name = "Bruce"
age = 26

IO.puts("Hi " <> name <> ".")
IO.puts("You're " <> Integer.to_string(age) <> " years old.")

Let’s run this program, and then I’ll explain. Use the following command to run the program:

$ elixir var.ex
Hi Bruce.
You're 26 years old.

Explanation

We’ve defined two variables, name and age, with initial values of Bruce and 26, respectively.

  • Bruce is string in Elixir and due to this, all the functions associated with type String are applicable to name variable.
  • 26 is integer in Elixir and due to this, all the the functions associated with type Integer are applicable to age variable.

In the last two lines, we’re printing text to the terminal.

  • In the first printing line, we’re greeting Bruce with “Hi Bruce.”. The <> is a binary concatenation operator used to combine strings.
  • In the second printing line, we’re printing the age message as “You’re 26 years old.”. Since age is an integer, we need to convert the number into a string. To do this, we use the to_string function from the Integer module.

---

Finally, there are two other primitive data types that I haven’t covered in this article which are Boolean and floating point numbers. Boolean has true and false value and floating numbers are numbers with decimal points.

Tags: elixir