How to do a complex function inside backtrips

I would like to perform multiple instructions and conditions within the backtrips how to do it

Backtrips? Do you mean back ticks? → `

sorry it is back ticks

Do you have some code to share?

Okay, I’m just going to guess what you want.

It sounds like you’re trying to do something like this:

`this is a ${if (Math.random() > 0.5) {
  return "good"
return "bad"
} string`

Which doesn’t work, because the placeholders inside ${} must be a single expression.

function goodOrBad() {
  if (Math.random() > 0.5) {
    return "good"
  return "bad"

// this works
`this is a ${goodOrBad()} string`

// this works (but ternaries are hard to read)
`this is a ${Math.random() > 0.5 ? "good" : "bad"} string`

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.