Yoolk Liquid

Home Basics Truthy-and-falsy

Truthy and Falsy in Liquid

In programming, we describe “truthy” and “falsy” as anything that returns true or false, respectively, when used inside an if statement

What is truthy?

All values in Liquid are truthy, with the exception of nil and false.

In the example below, the text “Tobi” is not a boolean, but it is truthy in a conditional

{% assign tobi = 'Tobi' %}
{% if tobi %}
  This will always be true.
{% endif %}

Strings, even when empty, are truthy. The example below will result in empty HTML tags if email.value is empty:

Input

{% assign email = listing.email %}
{% if email.value %}
  <h1>{{ email.value }}</h1>
{% endif %}

Output

<h1></h1>

To avoid this, you can check to see if the string is blank, as follows:

Output

{% assign email = listing.email %}
{% unless email.value == blank %}
  <h1>{{ email.value }}</h1>
{% endunless %}

What is falsy?

The only values that are falsy in Liquid are nil and false.

nil is returned when a Liquid object doesn’t have anything to return. For example, if a catalog_item doesn’t have an image, catalog_item.image will be set to nil. Since that is “falsy”, you can do this:

{% if catalog_item.image %}
  <!-- output catalog_item image -->
{% endif %}

The value false is returned through many Liquid object properties such as product.delivery.

Summary

The table below summarizes what is truthy or falsy in Liquid

truthy falsy
true x
false x
nil x
string x
empty string x
0 x
1 or 2 or 3.14 x
array x
empty array x
listing x
listing with no products x
page x