jacobdo
jacobdo

Reputation: 1615

How to prevent a conditional in a template literal from rendering false

I have the following template literal

`<div class="date ${($.inArray(date, dates) == -1) && `outside`}">
     <div>${date.format('D')}</div>
 </div>`

When the expression ($.inArray(date, dates) == -1) evaluates to true, the class outside shows up and nothing else, however, when it evaluates to false, false is outputted as a result of ${($.inArray(date, dates) == -1) &&outside}.

How could I avoid outputting false?

Upvotes: 2

Views: 1339

Answers (2)

Nisarg Shah
Nisarg Shah

Reputation: 14541

You can use a ternary conditional operator: condition ? valueWhenTrue : valueWhenFalse:

`<div class="date ${($.inArray(date, dates) == -1) ? `outside` : ``}">
    <div>${date.format('D')}</div>
</div>`

Upvotes: 2

M&#225;t&#233; Safranka
M&#225;t&#233; Safranka

Reputation: 4106

Ternary operator:

($.inArray(date, dates) == -1) ? 'outside' : ''

Fallback to empty string:

($.inArray(date, dates) == -1) && 'outside' || ''

Upvotes: 2

Related Questions