← Back to lesson 11: Ask precise true-or-false questions

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.

Original full example
# 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

  1. Read == / ===: Compare loosely or strictly.
  2. Read ||: Return the left value when it is truthy; otherwise return the right value.
  3. 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 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.

Original full exercise

Compare 2 with 2.0 twice, then give nil a fallback value.

Exercise starter
{2 ____ 2.0, 2 ____ 2.0, nil ____ :missing}

Return {true, false, :missing}.

Show hint

Use ==, ===, and || in that order.

Show original answer
Reference answer
{2 == 2.0, 2 === 2.0, nil || :missing}
Is 0 falsy in Elixir?

No. Only false and nil are falsy.

Original lesson takeaways
Design trade-offs and language comparisons
Original course references