Kishore
Kishore I am a Salesforce developer and a so-called blogger at SalesforceLwc.in. I love learning and sharing.

How to use expressions in lwc if:true if:false

How to use expressions in lwc if:true if:false

Expressions help us to conditionally render/display elements in markup

Expressions in Aura

<aura:if isTrue="{! (!v.attribute ? ifYes : ifFalse) }">
    <div>Conditional displayed elements/text</div>
</aura:if>

In Aura we can write expression as seen above, where as in lwc something like this is not possible to directly write in markup. We need write expressions in JavaScript and use their result in markup.

Expressions in LWC

<template>
    <div if:true={expression}>elements</div>
</template>
import { LightningElement } from 'lwc';
export default class SampleComponent {
    get expression() {
        return condition ? true : false;
    }
}

Note: In lwc if:true and if:false looks for boolean value either true or false

References