Skip to main content Link Search Menu Expand Document Toggle dark mode Copy Code (external link)

bool() : Convert to Boolean

Convert a value to a boolean (true/false).


On this page

Signature

bool(value)

Parameters

Parameter Type Required Description
value Any Yes The value to convert to boolean

Returns

Type: Boolean

true or false


Conversion Rules

Input Result
"true", "True", "TRUE" true
"false", "False", "FALSE" false
1, >0 (numbers) true
0 false
null false
Non-empty string true
Empty string false

Examples

String to Boolean

{{#if bool(model.isActiveString)}}
  <span class="active">Active</span>
{{else}}
  <span class="inactive">Inactive</span>
{{/if}}

Data:

doc.Params["model"] = new {
    isActiveString = "true"
};

Output:

<span class="active">Active</span>

Number to Boolean

{{#if bool(model.statusCode)}}
  <p>Operation successful</p>
{{else}}
  <p>Operation failed</p>
{{/if}}

Data:

doc.Params["model"] = new {
    statusCode = 1
};

Output:

<p>Operation successful</p>

Checkbox Value

{{#each model.features}}
  <div>
    {{#if bool(this.enabled)}}
      βœ“ {{this.name}} (Enabled)
    {{else}}
      βœ— {{this.name}} (Disabled)
    {{/if}}
  </div>
{{/each}}

Data:

doc.Params["model"] = new {
    features = new[] {
        new { name = "Email Notifications", enabled = "true" },
        new { name = "SMS Alerts", enabled = "false" },
        new { name = "Push Notifications", enabled = "1" }
    }
};

Output:

<div>
  βœ“ Email Notifications (Enabled)
</div>
<div>
  βœ— SMS Alerts (Disabled)
</div>
<div>
  βœ“ Push Notifications (Enabled)
</div>

Notes

  • Case-insensitive for string β€œtrue” and β€œfalse”
  • Numbers: 0 = false, anything else = true
  • Null values convert to false
  • Empty strings convert to false
  • Throws exception if value cannot be converted to boolean
  • Commonly used when data comes from external sources as strings

See Also