See all glossary terms

Truthy/Falsy

The keywords while and if only compare booleans. Any while or if receives an expression that evaluates to one of two values: true or false.
Most values in most (dynamic) programming languages can be coerced into true or false.
We call these values "truthy" and "falsy". If a value is truthy, it is considered true in a boolean context. If a value is falsy, it is considered false.
Truthy values are:
  • Any number other than zero, positive or negative, like 1, 2, 42, -13.
  • Non-empty strings like "hello", "a", "foo" are considered truthy also.
  • Any node or object.
  • Any dictionary with at least one element.
  • Any array with at least one element.
Falsy values are:
  • The number 0.
  • The empty string "".
  • The value null.
  • The empty array [].
  • The empty dictionary {}.
The process of automatically convert values for some expressions is called "type coercion". It's said that the value is coerced into a boolean.

Examples

The three snippets below are all equivalent. They all print "Words list is not empty." if the array words_list is not empty. The array is ["hello", "world"] in all cases, so the line appears in all cases.
In the first, we explicitly compare the size of the array to 0:
var words_list := ["hello", "world"]
if words_list.size() > 0:
	print("Words list is not empty.")
In the second, we use the implicit coercion of the size of the array to a boolean:
var words_list := ["hello", "world"]
if words_list.size():
	print("Words list is not empty.")
In the third, we use the implicit coercion of a non-empty array to a boolean:
var words_list := ["hello", "world"]
if words_list:
	print("Words list is not empty.")
Each example reaches the same result through different mechanisms.
These implicit conversions can be tricky, so we recommend to always be explicit (see the warning at the bottom of the type coercion article).

See Also

Related terms in the Glossary