Elixir basics · Lesson 10 (open contents)
Ask precise true-or-false questions
Compare values and separate truthy operators from strict Boolean operators.
Run this first
Run it once, change one input, and compare the new result.
# Compare equality and truthiness
value = 0
{value == 0.0, value === 0.0, value || :fallback, false or true}- The tuple is
{true, false, 0, true}; zero is truthy in Elixir.
Read from the first line down
- Read
== / ===: Compare loosely or strictly. - Read
||: Return the left value when it is truthy; otherwise return the right value. - Read
or: Combine Boolean values only.
Symbols are not secret signs
== / ===Compare loosely or strictly.
||Return the left value when it is truthy; otherwise return the right value.
orCombine Boolean values only.
Match each name to its meaning
truthy
Only false and nil are falsy in Elixir; every other value is truthy.
strict equality
=== compares both value and numeric type, while == allows integer-float equality.
Boolean operators
and, or, and not require Boolean inputs; &&, ||, and ! accept any value.
Why these forms are useful
Run the example first. Predict one result, then change one input and run it again.
Only false and nil are falsy in Elixir; every other value is truthy.
=== compares both value and numeric type, while == allows integer-float equality.
Close the answer and try
Compare 2 with 2.0 twice, then give nil a fallback value.
{2 ____ 2.0, 2 ____ 2.0, nil ____ :missing}Target result: Return {true, false, :missing}.
Stuck? Read one hint
Use ==, ===, and || in that order.
After you run it, see one answer
{2 == 2.0, 2 === 2.0, nil || :missing}Think about it: Is `0` falsy in Elixir?
No. Only false and nil are falsy.
Remember these three lines
- 1
Use
===when numeric type matters. - 2
Zero and empty collections are truthy.
- 3
Use strict Boolean operators only with Booleans.