liveBook · Manning

Truthiness

As explained in Elixir in Action, Third Edition

A truthy value is a value that Elixir treats as true in truthiness evaluation.

Truthiness determines how Elixir evaluates a value in short-circuit expressions. Only false and nil are falsy; every other value is truthy. section 2.4.2 section 2.5

Use the short-circuit operators according to the value they return:

  • || returns the first truthy expression, or the final expression when all expressions are falsy. section 2.4.2 section 2.5
  • && returns the first falsy expression; otherwise it returns the second expression. section 2.4.2 section 2.5
  • ! returns false for a truthy value and true for a falsy value. section 2.5

Fallbacks and guarded work

Chain fallback sources with read_cached() || read_from_disk() || read_from_database(). Guard dependent work with connection && read_data(connection); the second expression is skipped when connection is nil or false. section 2.4.2 section 2.4.2

:an_atom
:another_atom

Compare representative short-circuit expressions and their returned values. section 2.4.2

How Elixir in Action, Third Edition covers it

How deep it goes
Defines falsy values and operator return behavior, then stops short of broader language-reference detail.
Examples
Demonstrates cache, disk, and database fallback chains plus connection-guarded data access.
What it assumes
Assumes basic expressions and atoms while distinguishing Boolean atoms from the wider set of truthy values.
What you can do after
Apply short-circuit operators to write concise fallback chains and conditionally evaluate dependent expressions.
How it connects
Connects atoms, Boolean values, nil, logical operators, and short-circuit operators.

Also explained in

Compare how 6 books explain truthiness →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elixir in Action, Third Edition. The section links point to the parts of the book it draws on.