Articles in this section

isTruthy helper

The isTruthy helper returns true when the input has a meaningful value, and false when it is absent or empty. Use it to check that a field is present before you map it, or to drive a conditional block.

Syntax

{{isTruthy value}}

value -- The value to evaluate. Required.

Values that return false

Value Type
false Boolean
0 Integer
"" Empty string
null Null
undefined Undefined
NaN Not a number

All other values -- non-empty strings, non-zero numbers, arrays, and objects -- return true.

Examples

Evaluate a mix of present and absent values

{{isTruthy record.a}}
{{isTruthy record.b}}
{{isTruthy record.c}}
{{isTruthy record.d}}
{{isTruthy record.e}}

Input:

{
 "record": {
 "a": 1,
 "b": 0,
 "c": "hello",
 "d": "",
 "e": null
 }
}

Output:

true
false
true
false
false

record.a (1) and record.c ("hello") have meaningful values, so they return true. record.b (0), record.d (""), and record.e (null) are absent or empty, so they return false.

Use in a conditional block

{{#if (isTruthy record.status)}}
 Active
{{else}}
 Inactive
{{/if}}

Input:

{ "record": { "status": "enabled" } }

Output: Active

Wrap isTruthy in parentheses to use it as an inline expression inside #if. The same template renders Inactive when record.status is "".

Usage notes

  • Wrap isTruthy in parentheses when using it as an inline expression: {{#if (isTruthy value)}}.
  • The string "0" and the string "false" are non-empty strings and return true. Only the integer 0 and the boolean false return false.
  • To check whether a value is absent rather than present, use the isFalsey helper instead.
  • #some accepts isTruthy as a condition name. That test applies to each element of an array. See the #some helper.