---
title: "Do not use Ternary operator to return Boolean"
url: "https://blog.beyondthecloud.dev/blog/do-not-use-ternary-operator-to-return-boolean"
author: "Piotr Gajek"
published: "2023-12-08"
updated: "2025-02-27"
tags: "salesforce code review"
---

# Do not use Ternary operator to return Boolean

By [Piotr Gajek](https://blog.beyondthecloud.dev/author/piotr-gajek.md), December 8, 2023

![do-not-use-ternary-operator-to-return-boolean](https://wordpress.beyondthecloud.dev/wp-content/uploads/2023/12/do-not-use-ternary-operator-to-return-boolean.png)

## Introduction

All comperation statements either in Apex or LWC return Boolean. Let’s take a look for an examples.

**LWC**

```js
'a' === 'b' // => false
1 === 1 // => true
```

**Apex**

```apex
'a' == 'b' // => false
1 == 1 // => true
```

## Issue

There is no need to use the ternary operator since the comparison statement returns a Boolean.

❌❌❌

```js
const isPartner = this.userType === 'Partner' ? true : false;
```

```js
const isCustomer = this.userType === 'Partner' ? false : true;
```

Based on the [documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional%5Foperator): 

```js
condition ? exprIfTrue : exprIfFalse;
```

You have something like this:

```js
condition that evaluates to Boolean (condition) ? true (exprIfTrue) : false (exprIfFalse);
```

```js
true (condition that evaluates to Boolean) ? true : false;
```

```js
false (condition that evaluates to Boolean) ? true : false;
```

## Best Practice

* `this.userType === 'Partner'` is comperation statement.
* A comparison statement inherently returns a Boolean value.
* Therefore, there is no need to use the [Ternary operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional%5Foperator), since the result is already `true` or `false`.

Based on the [KISS principle](https://en.wikipedia.org/wiki/KISS%5Fprinciple) your code can be as simple as possible.

✅✅✅

```js
const isPartner = this.userType === 'Partner';
```

```js
const isCustomer = this.userType !== 'Partner';
```

**Do not use Ternary operator to return Boolean**

---

If you have any questions feel free to ask in the comment section below. 🙂

Was it helpful? Check out our other great posts [here](https://beyondthecloud.dev/blog).

---

## References

* [KISS principle](https://en.wikipedia.org/wiki/KISS%5Fprinciple)
* [Ternary operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional%5Foperator)
