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
Data:
doc.Params["model"] = new {
isActiveString = "true"
};
Output:
<span class="active">Active</span>
Number to Boolean
Data:
doc.Params["model"] = new {
statusCode = 1
};
Output:
<p>Operation successful</p>
Checkbox Value
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