Sub topic 3
How a condition is evaluated: truthy and falsy values
This chapter assumes you know the basics of an LWC template and its JavaScript class, including curly-brace binding and getters. You will learn how lwc:if decides whether to render a block, and which values count as true or false.
The directive tests truthiness
The value you bind to lwc:if does not have to be a boolean. LWC evaluates it the way JavaScript evaluates the condition of an if statement. A value that converts to true is truthy and the block renders. A value that converts to false is falsy and the block is removed from the DOM.
The binding must still be a property or getter reference such as {isReady}. Expressions like {count > 0} or {!isReady} are not allowed in a template. The next chapters cover getters, which handle this.
The falsy values
JavaScript has a short list of falsy values. Every other value is truthy.
| Value | Type | Result in `lwc:if` |
|---|---|---|
| `false` | boolean | falsy |
| `0`, `-0`, `0n` | number, BigInt | falsy |
| `""` (empty string) | string | falsy |
| `null` | null | falsy |
| `undefined` | undefined | falsy |
| `NaN` | number | falsy |
| `"0"`, `"false"` | non-empty string | truthy |
| `[]` | empty array | truthy |
| `{}` | empty object | truthy |
| `-1`, `42` | number | truthy |
Seeing it in a component
This component binds fields of different types directly to lwc:if.
import { LightningElement } from 'lwc';
export default class TruthyDemo extends LightningElement {
count = 0;
name = 'Ada';
items = [];
account; // undefined until it is assigned
}<template>
<p lwc:if={count}>Count is {count}.</p>
<p lwc:else>Count is zero, so this fallback renders.</p>
<p lwc:if={name}>Hello, {name}.</p>
<p lwc:if={items}>The array is truthy, even though it is empty.</p>
<p lwc:if={account}>Account loaded.</p>
<p lwc:else>No account yet.</p>
</template>With these values, the page shows the zero-count fallback, the greeting, the empty-array message, and "No account yet." The count and account blocks are falsy. The items block renders because an empty array is an object, and every object is truthy.
Checking for content, not existence
Binding an array directly is the most common truthiness mistake. A list that has loaded but has no records is [], which is truthy, so the block renders with nothing in it. To test whether a list has entries, compare its length in a getter.
get hasItems() {
return this.items.length > 0;
}<template>
<p lwc:if={hasItems}>You have items.</p>
<p lwc:else>Your list is empty.</p>
</template>Strings need similar care. A string of spaces, such as " ", is truthy. If user input may contain only whitespace, trim it in a getter first.
Reactivity and truthiness
When a bound field changes, the template re-evaluates the condition. Only the result matters. If count changes from 1 to 2, both values are truthy, so the block stays in place and only its text updates. The block is added or removed only when the value crosses between truthy and falsy.
Exercise
A component has records = [] after a successful query that returned no rows. The template contains <p lwc:if={records}>Results found</p>. What renders, and how would you fix it?
Show answer
"Results found" renders, because an empty array is truthy. Add a getter such as get hasRecords() { return this.records.length > 0; } and bind lwc:if={hasRecords} instead.
Exercise
Which of these values make an lwc:if block render: 0, "0", null, {}, "", -1?
Show answer
"0", {}, and -1 render the block. 0, null, and "" are falsy.