# How do you do conditional rendering and list iteration in LWC templates?

**Topic:** Lwc · **Difficulty:** Easy · **Tags:** lwc, templates, conditional

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-you-do-conditional-rendering-and-list-iteration-in-lwc-templates

## Answer

**Conditional rendering**
```html
<template lwc:if={isLoading}>Loading…</template>
<template lwc:elseif={hasError}>Error</template>
<template lwc:else>Content</template>
```
(The older `if:true` / `if:false` directives still work but `lwc:if`, `lwc:elseif`, `lwc:else` are the current recommendation.)

**Iteration**
```html
<template for:each={contacts} for:item="c">
  <li key={c.Id}>{c.Name}</li>
</template>
```
Also `iterator:it={contacts}` when you need `it.first` / `it.last`.

**Rules:** every iterated top-level element needs a unique, stable **key** (not the index), and templates only accept simple property references, so compute logic in JS getters.

---
Source: ForceChallenges · last updated 2026-10-21
