Sub topic 7
Providing a fallback with lwc:else
This chapter assumes you can write a Lightning Web Component with an HTML template and a JavaScript class. It also assumes you know how to bind a property with curly braces and how lwc:if shows content when a condition is truthy. Here you add the fallback branch.
What lwc:else does
lwc:else renders its block when the preceding lwc:if (or lwc:elseif) condition is falsy. It is the "otherwise" branch. Together with lwc:if, it guarantees that exactly one of the two blocks is in the DOM.
Unlike lwc:if, the directive takes no value. You write lwc:else and nothing after it. It has no expression to evaluate, because it runs whenever no earlier branch did.
A first example
Here the directives sit directly on elements, which suits the case where only one element changes.
<template>
<p lwc:if={hasItems}>You have {itemCount} items.</p>
<p lwc:else>Your list is empty.</p>
</template>The component class supplies the two values.
import { LightningElement } from 'lwc';
export default class ItemSummary extends LightningElement {
items = [];
get hasItems() {
return this.items.length > 0;
}
get itemCount() {
return this.items.length;
}
}With an empty array, hasItems is false, so the engine renders only the second paragraph. When items is reassigned to a non-empty array, the first paragraph is created and the second is removed.
Where the else branch must go
The engine reads the directives as one linked block. That only works if the branches are adjacent siblings. The diagram shows how the engine picks a branch.
Follow these placement rules:
lwc:elsemust come immediately after anlwc:iforlwc:elseifsibling.- Nothing may sit between the two, apart from whitespace. A stray element breaks the chain.
lwc:elsetakes no value.lwc:else={something}is invalid.- A block can have at most one
lwc:else, and it must be last.
The directive works on a <template> tag too. Use that form when a branch holds several elements:
<template>
<template lwc:if={isLoggedIn}>
<h2>Welcome back</h2>
<button onclick={handleLogout}>Log out</button>
</template>
<template lwc:else>
<h2>Please sign in</h2>
<button onclick={handleLogin}>Log in</button>
</template>
</template>The <template> wrapper adds nothing to the DOM. Only its children appear.
import { LightningElement } from 'lwc';
export default class AuthBanner extends LightningElement {
isLoggedIn = false;
handleLogin() {
this.isLoggedIn = true;
}
handleLogout() {
this.isLoggedIn = false;
}
}Each click reassigns the field. The template re-evaluates, removes one branch, and creates the other.
Falsy means fallback
The fallback runs for any falsy result: false, 0, '', null, undefined, or NaN. This matters when you bind a field directly:
- If
messageis'', thelwc:elseblock renders. - If
countis0, thelwc:elseblock renders, even though0may be a valid number to show.
When a zero or empty string is meaningful, compute an explicit boolean in a getter instead of binding the raw value.
Replacing if:false
In the older syntax, the fallback was a second test of the same property with if:false. That meant two evaluations that could drift apart if someone edited only one. With lwc:else, the fallback is tied to the lwc:if above it by position.
<template>
<template lwc:if={isVisible}>
<p>Visible</p>
</template>
<template lwc:else>
<p>Hidden</p>
</template>
</template>Check your understanding
Exercise
A component has <p lwc:if={count}>Count: {count}</p> followed by <p lwc:else>No items</p>. The field count is 0. Which paragraph renders, and how would you make 0 display as "Count: 0" instead?
Show answer
The lwc:else paragraph ("No items") renders, because 0 is falsy. To show the count when it is zero, bind to a getter that returns an explicit boolean, for example get hasCount() { return this.count !== null && this.count !== undefined; }, and use lwc:if={hasCount}.
Exercise
Why is <p lwc:else={isHidden}>Hidden</p> invalid?
Show answer
lwc:else takes no value. It renders whenever the earlier branches in the chain did not, so there is no condition to evaluate. Write <p lwc:else>Hidden</p>.