# How can I rewrite this with Ternary Operator?

**URL:** <https://forum.kirupa.com/t/how-can-i-rewrite-this-with-ternary-operator/638755>\
**Category:** Uncategorized\
**Created:** [October 3, 2018, 4:41pm UTC](https://forum.kirupa.com/t/how-can-i-rewrite-this-with-ternary-operator/638755 "2018-10-03T16:41:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kayut](https://avatars.discourse-cdn.com/v4/letter/k/bcef8e/32.png) [@kayut](https://forum.kirupa.com/u/kayut)\
**Post date:** [October 3, 2018, 4:41pm UTC](https://forum.kirupa.com/t/how-can-i-rewrite-this-with-ternary-operator/638755/1 "2018-10-03T16:41:15Z")

</div>

Hi,  
How can I rewrite this with Ternary Operator?

```auto
face.textContent = result == 0 ? headNr + ' times ' + resultFace : tailNr + ' times ' + resultFace;
```

I tried everything, but couldn’t find a way.

Thanks

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [October 3, 2018, 4:54pm UTC](https://forum.kirupa.com/t/how-can-i-rewrite-this-with-ternary-operator/638755/2 "2018-10-03T16:54:28Z")

</div>

It’s already using it. Do you mean _without_ using the ternary operator?

---

<div class="post-metadata">

**Author:** ![kayut](https://avatars.discourse-cdn.com/v4/letter/k/bcef8e/32.png) [@kayut](https://forum.kirupa.com/u/kayut)\
**Post date:** [October 3, 2018, 5:42pm UTC](https://forum.kirupa.com/t/how-can-i-rewrite-this-with-ternary-operator/638755/3 "2018-10-03T17:42:33Z")

</div>

Sorry, I tried so many different things that I asked the wrong question in confusion.  
Actually, I wanted to rewrite this ternary operation with Template Literal.  
I tried the following but it’s not working:

```auto
face.textContent = `${result} == 0 ? ${headNr} times ${resultFace} : ${tailNr} times ${resultFace}`;
```

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [October 3, 2018, 6:00pm UTC](https://forum.kirupa.com/t/how-can-i-rewrite-this-with-ternary-operator/638755/4 "2018-10-03T18:00:09Z")

</div>

There are a couple of ways to do this. The thing to keep in mind is that everything in `…` that is not within `${...}` is considered a string. The only way the ternary will work if you try to wrap the whole thing in a template literal is if you also wrap the whole thing in a single template literal expression.

```auto
`${result == 0 ? headNr + ' times ' + resultFace : tailNr + ' times ' + resultFace}`;

```

but that defeats the purpose.

So instead, you’ll just use template literals where you have strings, adding in the expressions where you have variables.

```auto
result == 0 ? `${headNr} times ${resultFace}` : `${tailNr} times ${resultFace}`;

```

You can simplify this further by breaking out the duplication and using a falsy check for result.

```auto
`${result ? tailNr : headNr} times ${resultFace}`;

```
