Build Your Course The Easy Way
Get Started Free

Truthy and Falsy

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

In this article:

What is Truthy?

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

In the example below, the text “Dr. O' Ducklehan” is not a boolean, but it is truthy in a conditional:

{% assign name = "Dr. O' Ducklehan" %} {% if name %} <p>Nice to meet you, I'm {{ name }}.</p> {% endif %} Strings, even when empty, are truthy. The example below will result in empty HTML tags if post.body is empty:

{% if post.body %} <div> {{ post. body }} </div> {% endif %} To avoid this, you can check to see if the string is blank, as follows:

{% unless post.body == blank %} <div> {{ post. body }} </div> {% endif %}

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 post doesn't have a poster image, post.posterimageurl will be set to nil. Since that is “falsy”, you can do this:

{% if post.poster_image_url %} <img src="{{ post.poster_image_url }}"> {% endif %}

Contact support or access our 24/7 Live Chat:

Contact Support Live Chat

Need Help? Live Chat with Us.