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

# Use-Ternary-Operator

#### Sourcery rule id: `use-ternary-operator`

#### Description

Replace if statement with ternary operator


#### Before

```javascript
if (cond) {
    var x = 1
} else {
    var x = 2
}
```

#### After

```javascript
var x = cond ? 1 : 2;
```



#### Explanation

The ternary operator is a simpler and more elegant way to write conditional assignments
to a variable than the long form.
