2019-10-21 04:22:28 -04:00
---
2023-05-22 10:43:12 -04:00
title: cond
2023-08-30 13:23:47 -04:00
description: Returns one of two arguments depending on the value of the control argument.
2019-10-21 04:22:28 -04:00
categories: [functions]
2023-08-30 13:23:47 -04:00
keywords: [conditional, ternary]
2019-10-21 04:22:28 -04:00
menu:
docs:
2023-05-22 10:43:12 -04:00
parent: functions
2023-08-30 13:23:47 -04:00
signature: [cond CONTROL ARG1 ARG2]
2019-10-21 04:22:28 -04:00
relatedfuncs: [default]
---
2023-08-30 13:23:47 -04:00
The CONTROL argument is a boolean value that indicates whether the function should return ARG1 or ARG2. If CONTROL is `true` , the function returns ARG1. Otherwise, the function returns ARG2.
2019-10-21 04:22:28 -04:00
2023-05-22 10:43:12 -04:00
```go-html-template
2023-08-30 13:23:47 -04:00
{{ $qty := 42 }}
{{ cond (le $qty 3) "few" "many" }} → "many"
2019-10-21 04:22:28 -04:00
```
2023-08-30 13:23:47 -04:00
The CONTROL argument must be either `true` or `false` . To cast a non-boolean value to boolean, pass it through the `not` operator twice.
```go-html-template
{{ cond (42 | not | not) "truthy" "falsy" }} → "truthy"
{{ cond ("" | not | not) "truthy" "falsy" }} → "falsy"
```
2019-10-21 04:22:28 -04:00
2023-05-22 10:43:12 -04:00
{{% note %}}
2023-08-30 13:23:47 -04:00
Unlike [ternary operators] in other languages, the `cond` function does not perform [short-circuit evaluation]. The function evaluates both ARG1 and ARG2, regardless of the CONTROL value.
2019-10-21 04:22:28 -04:00
2023-08-30 13:23:47 -04:00
[short-circuit evaluation]: https://en.wikipedia.org/wiki/Short-circuit_evaluation
[ternary operators]: https://en.wikipedia.org/wiki/Ternary_conditional_operator
2023-05-22 10:43:12 -04:00
{{% /note %}}
2023-08-30 13:23:47 -04:00
Due to the absence of short-circuit evaluation, these examples throw an error:
```go-html-template
{{ cond true "true" (div 1 0) }}
{{ cond false (div 1 0) "false" }}
```