On Elixir Pt. 2
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.
Bruceis string in Elixir and due to this, all the functions associated with typeStringare applicable tonamevariable.- 26 is integer in Elixir and due to this, all the the functions associated with type
Integerare applicable toagevariable.
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
agemessage as “You’re 26 years old.”. Sinceageis an integer, we need to convert the number into a string. To do this, we use theto_stringfunction from theIntegermodule.
---
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.