Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Basic Types

01-basic-types.livemd

Basic Types

Hello World

IO.puts("Hello not from World")

Basic Types

Integers

10 # integer
0x3F # integer as hex

Float

105.67

Atom

Atoms are like Symbols in JS(similar to consts). In elixir atoms are used heavily for differentiating different values like :ok or :error

:ok
:error
:sunny_weather

There are several special atoms we use for booleans and nulls πŸ‘‡

> You should avoid creating atoms at runtime most of the time

Null values

nil # this is NULL in JS

Booleans

true # true
false # false

To compare values we can use ==

true == false

and we can also use or/2, and/2, not/1

true and false
true or false
not true

We can also use ||/2, &&/2, !/1 to evaluate expressions.

These operators will consider any false or nil values as falsy, everything else will be just truthy

> πŸ’‘ In some languages like JS 0 and "" considered as NULL but in Elixir there are no assumptions made

These are short circuit operators(similar to JS)

120 || false # second value never checked as first one is truthy
10 && true # because 10 is truthy it returns second value
nil && true # returns nil because first value is falsy
!nil # you know this

Strings

Double quotes are β€œstrings”

"WaveZync 🌊"

String concatenate can be done using <> operator

"WaveZync 🌊 " <> "❀️" <> " Elixir"

String interpolation can be done as well.

this is similar to following in JS

const orderId = 105;
const notification = `Order ${orderId} delivered πŸ’ͺ`

in elixir

order_id = 105
notification = "Order #{order_id} delivered πŸ’ͺ"

Comparision

Elixir supports ==, !=, <=, >=, < and >

"apples" != :apples
100 >= 10

These comparisions are structural

in simple terms they compare underlying data structure(in a string its bytes)

"kasun" > "wavezync"

Term Ordering

This is how elixir gives precedence to comparision by data types

number < atom < reference < function < port < pid < tuple < map < list < bitstring

Variables

We use snake_case for variables

order_id = 105
username = "kasun"
transaction_id = "11"
next_step = :rating
food_item = "Pizza πŸ•"

We can inspect any variable with IO.inspect

IO.inspect([order_id, username, transaction_id, food_item, next_step])

IO.inspect is a powerful tool

If variable is unused we can use _ to ignore

_ordered_at = Date.utc_today()