> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ubiquex.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Conditional Expressions

> 10 types of conditional expressions in ubx — ternary, null coalescing, when, and more.

ubx supports 10 types of conditional expressions. All types propagate `Pending&lt;T&gt;` — any expression involving a `~` reference becomes `Pending&lt;T&gt;` and defers to apply time via `.apply()`.

## Type 1 — `??` Null Coalescing

Returns the left operand if non-null/non-empty; otherwise the right.

```hcl theme={null}
unit "aws_s3_bucket_v2" "b" {
  bucket = input.custom_name ?? "default-bucket"
}
```

```typescript theme={null}
bucket: (customName ?? "default-bucket"),
```

## Type 2 — Ternary `? :`

Classic conditional expression.

```hcl theme={null}
unit "aws_rds_instance" "db" {
  instance_class = input.env == "prod" ? "db.r6g.xlarge" : "db.t3.micro"
}
```

```typescript theme={null}
instanceClass: (env === "prod") ? "db.r6g.xlarge" : "db.t3.micro",
```

## Type 3 — `when` Block Modifier

Conditionally creates an entire resource block.

```hcl theme={null}
input "enable_cache" { default = "true" }

unit "aws_elasticache_replication_group" "cache" {
  when                  = input.enable_cache == "true"
  replication_group_id  = "my-cache"
  num_cache_clusters    = 1
}
```

The resource is only included in the plan when the condition is true.

## Type 4 — `when` on output Blocks

```hcl theme={null}
output "replica_endpoint" {
  value = ~unit.aws_rds_instance.replica.endpoint
  when  = input.env == "prod"
}
```

Compiles to a ternary:

```typescript theme={null}
export const replica_endpoint = ((env === "prod"))
  ? replica.endpoint
  : pulumi.output(undefined);
```

## Type 5 — `&&` and `||` in Conditions

```hcl theme={null}
unit "aws_s3_bucket_v2" "logs" {
  when   = input.env == "prod" && input.enable_logs == "true"
  bucket = "prod-logs"
}
```

## Type 6 — Comparison Operators

```hcl theme={null}
instance_class = input.node_count > 3 ? "db.r6g.xlarge" : "db.t3.micro"
```

Supported: `==`, `!=`, `>`, `<`, `>=`, `<=`

## Type 7 — `!` Negation

```hcl theme={null}
unit "aws_s3_bucket_v2" "dev_bucket" {
  when   = !(input.env == "prod")
  bucket = "dev-assets"
}
```

## Type 8 — Nested Ternary

```hcl theme={null}
instance_class = input.env == "prod"
  ? "db.r6g.xlarge"
  : input.env == "staging"
    ? "db.t3.small"
    : "db.t3.micro"
```

## Type 9 — `Pending&lt;T&gt;` in Condition

When a condition contains a `Pending&lt;T&gt;` value, the result is `Pending&lt;T&gt;` and resolves at apply time:

```hcl theme={null}
unit "aws_s3_bucket_v2" "replica" {
  bucket = ~unit.aws_s3_bucket_v2.primary.bucket ?? "fallback-bucket"
}
```

```typescript theme={null}
bucket: primary.bucket.apply(b => b ?? "fallback-bucket"),
```

## Type 10 — `??` in String Interpolation

```hcl theme={null}
unit "aws_s3_bucket_v2" "b" {
  bucket = "${input.prefix ?? "default"}-assets"
}
```

```typescript theme={null}
bucket: `${(prefix ?? "default")}-assets`,
```
