# How do getter properties function in LWC JavaScript controllers for dynamic UI binding?

**Topic:** LWC · **Difficulty:** Medium · **Tags:** LWC, Getters, Data Binding

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-getter-properties-function-in-lwc-javascript-controllers-for-dynamic-ui-binding

## Answer

**Basics:** LWC templates only allow simple property access such as `{contact.Name}`. They do not allow `{a + b}`, `{!flag}` or function calls. When you need a derived value, you write a getter in the class and reference it in the template like any property: `{fullName}`.

**How it works:**
1. Define `get fullName() { return `${this.first} ${this.last}`; }`.
2. Use it as `{fullName}` or in directives: `<template lwc:if={hasItems}>`.
3. During render, the engine reads the getter and records which reactive fields it touched.
4. When any of those fields change, the component re-renders and the getter runs again.
5. A getter cannot take arguments, so for per-row values in a loop you precompute properties on each item while mapping the data.

**Advanced / deep dive:** Getters should be pure and cheap: no state changes, no server calls, no side effects. Because they run every render, an expensive computation should be cached in a field and recomputed only when its inputs change (for example in a setter or when data arrives). Returning a new array or object from a getter on every call creates a new reference each render, which can make child components re-render needlessly and break `@api` equality assumptions. For public computed properties, combine `@api` with a getter and setter, where the setter normalizes input. Getters can also expose class names conditionally for SLDS: `get cls() { return this.active ? 'slds-button slds-button_brand' : 'slds-button'; }`, avoiding string logic in markup. Getters on tracked structures react to deep mutation only when the field is `@track` or immutably updated.

**Code / config example:**
```javascript
import { LightningElement } from 'lwc';
export default class OrderSummary extends LightningElement {
    items = [{ id: 1, price: 10, qty: 2 }, { id: 2, price: 5, qty: 1 }];
    discountApplied = false;

    get total() {
        const sum = this.items.reduce((s, i) => s + i.price * i.qty, 0);
        return this.discountApplied ? sum * 0.9 : sum;
    }
    get hasItems() { return this.items.length > 0; }
    get badgeClass() { return this.discountApplied ? 'slds-badge slds-theme_success' : 'slds-badge'; }
}
```
```html
<template lwc:if={hasItems}>
    <span class={badgeClass}>Total: {total}</span>
</template>
```

**Hidden gotchas & must-remember points:**
- Getters take no parameters; use precomputed fields for loop items.
- They run on every render, so keep them cheap or cache the result.
- Never change state or call Apex inside a getter.
- Returning a fresh object or array every call can trigger needless child re-renders.
- A getter reading a plain object's nested field updates reactively only if that object is tracked or replaced.
- Decorate only one of a getter/setter pair with `@api`.

**Best practice:** Use small, pure getters for formatting and conditions, and cache any expensive result.

---
Source: ForceChallenges · last updated 2026-11-02
