Skip to main content Link Search Menu Expand Document (external link)

bool() : Convert to Boolean


On this page

Summary

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

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


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

  <span class="inactive">Inactive</span>

Data:

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

Output:

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

Number to Boolean


  <p>Operation successful</p>

  <p>Operation failed</p>

Data:

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

Output:

<p>Operation successful</p>

Checkbox Value


  <div>
    
      ✓  (Enabled)
    
      ✗  (Disabled)
    
  </div>

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