Ask precise true-or-false questions: full examples and further reading
The original lesson material is preserved here for reference. The main notebook focuses on this lesson's core question.
Compare values and separate truthy operators from strict Boolean operators.
Run four comparisons and predict which values are true, false, or passed through.
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.
Steps for the full example
- 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.
Original lesson notes
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.
Symbols and terms
== / ===- Compare loosely or strictly.
||- Return the left value when it is truthy; otherwise return the right value.
or- Combine Boolean values only.
- truthy
- Only
falseandnilare 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, andnotrequire Boolean inputs;&&,||, and!accept any value.
Original full exercise
Compare 2 with 2.0 twice, then give nil a fallback value.
{2 ____ 2.0, 2 ____ 2.0, nil ____ :missing}Return {true, false, :missing}.
Show hint
Use ==, ===, and || in that order.
Show original answer
{2 == 2.0, 2 === 2.0, nil || :missing}Is 0 falsy in Elixir?
No. Only false and nil are falsy.
Original lesson takeaways
- Use
===when numeric type matters. - Zero and empty collections are truthy.
- Use strict Boolean operators only with Booleans.