Comparison
Elixir also provides ==, !=, <=, >=, < and > as comparison operators. We can compare numbers, atoms, strings, booleans, etc. Integers and floats compare the same if they have the same value, but you can use the strict comparison operator === and !== if you want to distinguish between integers and floats.
Basic equality:
1 == 1
# 💡 Try changing `2.0` to `1.0`. Are they equal now?
1 == 2.0
String comparison:
# 💡 Try changing `"bar"` to `"foo"`. What's the result now?
"foo" == "bar"
Strict equality distinguishes integers from floats:
# 💡 Try changing `===` to `==`. Does the result change?
1 === 1.0
Basic inequality:
2 > 10
The comparison operators in Elixir can compare across any data type. It means you can, for example, compare strings with integers. It’s useful in cases like sorting algorithms, but there are caveats - you can read more about it here.
"abc" > 1
Atoms are greater than numbers:
# 💡 Try changing the atom name.
:infinity > 1