Skip to content

Truthy and Falsy Values

The rendering system often performs automatic type conversions, especially when evaluating conditional expressions. For instance, a string with the value "0" is considered truthy, whereas the numeric value 0 is evaluated as falsy. In a boolean context, values such as null, undefined, false, 0, or an empty string are treated as falsy and will evaluate to false, regardless of their original data type. Recognizing these implicit conversions is essential to prevent logical errors and ensure consistent and reliable application behavior.

Types of Evaluation

Truthy

  1. Non-zero Numbers: Any number that isn't zero.
  2. Non-empty Strings: Any string that isn’t empty or only whitespace.
  3. Non-empty Arrays: Arrays containing one or more items.
  4. Non-empty Dictionaries: Dictionaries with one or more key-value pairs.
  5. Boolean true: Explicitly the boolean value true.
  6. Non-blank Objects: Objects which are not nil, empty, or blank.

Falsey

  1. The Number Zero: The integer 0.
  2. Empty Strings: Strings that are empty or contain only whitespace.
  3. Empty Arrays: Arrays with no items.
  4. Empty Dictionaries: Dictionaries with no key-value pairs.
  5. Boolean false: Explicitly the boolean value false.
  6. Nil Values: A value representing null or absence (nil).
  7. Blank Values: Values representing "nothing".

Use Cases

Non-zero Number

    {% assign a = 10 %}
    {% if a %}
      Number is > 0 .
    {% endif %}
Output:
    Number is > 0

Non-empty String

    {% assign text = "Hello, World!" %}
    {% if text %}
      String length > 0.
    {% endif %}
Output:
    String length > 0.

Non-empty Array

    {% assign items = "item1, item2, item3" | split: ", " %}
    {% if items %}
      Array length > 0
    {% endif %}
Output:
    Array length > 0

Non-empty Dictionary

    {% assign user = {"name": "John Doe", "age": 30} %}
    {% if user %}
      Dictionary length > 0.
    {% endif %}
Output:
    Dictionary length > 0.

Boolean true

    {% assign condition = true %}
    {% if condition %}
      bool value = true
    {% endif %}
Output:
    bool value = true.

Zero Number

    {% assign a = 0 %}
    {% if a %}
      Number > 0
    {% else %}
      Number = 0
    {% endif %}
Output:
    Number = 0

Empty String

    {% assign text = "" %}
    {% if text %}
      string is not empty
    {% else %}
      string is empty
    {% endif %}
Output:
    string is empty

Empty Array

    {% assign items = "" | split: ", " %}
    {% if items %}
        Array is not empty
    {% else %}
      Array is empty
    {% endif %}
Output:
    Array is empty.

Empty Dictionary

    {% assign user = {} %}
    {% if user %}
      Dictionary is not empty
    {% else %}
      Dictionary is empty
    {% endif %}
Output:
    Dictionary is empty.

Boolean false

    {% assign condition = false %}
    {% if condition %}
      Boolean is true
    {% else %}
      Boolean is false
    {% endif %}
Output:
   Boolean is false.

Nil Value

    {% assign value = nil %}
    {% if value %} 
       Nil is true
    {% else %}
      Nil is false
    {% endif %}
Output:
    Nil value is falsy.

Blank Value

    {% assign value = blank %}
    {% if value %}
      Blank value is true
    {% else %}
      Blank value is false
    {% endif %}
Output:
    Blank value is false

Default Operator

Using the default operator allows for elegant handling of false value checks by providing a fallback value when the original value is considered falsy, such as null, undefined, false, or 0. This simplifies code by ensuring that variables always have a valid value, reducing the need for verbose conditional statements and improving readability.

Example:

    {% assign value = nil %}
    {{ value | default: "Default Value" }}
Output:
    Default Value