# What is Lightning Web Components (LWC), and how does its architecture differ from Aura components?

**Topic:** LWC · **Difficulty:** Medium · **Tags:** LWC, Architecture, Web Components

Read it on the site: https://www.forcechallenges.com/interview-questions/what-is-lightning-web-components-lwc-and-how-does-its-architecture-differ-from-aura-components

## Answer

**Basics:** Both build Lightning UI (record pages, apps, utility bar, quick actions, Experience Cloud). Aura came first (2014); LWC arrived in 2019 and relies on what browsers do natively, so there is less framework code to download and run.

**How it works:**
- LWC bundle: `.html` template, `.js` class, optional `.css` and `.js-meta.xml` for exposure targets.
- Decorators: `@api` (public property or method), `@wire` (reactive data from Apex or the Lightning Data Service), `@track` (only needed for deep mutation tracking of objects or arrays).
- Communication: properties down, `CustomEvent` up, and Lightning Message Service for unrelated components.
- Aura uses `.cmp` markup, controller and helper JS, and application or component events.

**Advanced / deep dive:** Performance: LWC avoids Aura's heavy virtual component layer, and uses standard `lwc:if` directives (legacy `if:true` still works). Security: Lightning Web Security (LWS) is the newer sandbox that replaces Lightning Locker. Interop: an Aura component can contain LWC, but LWC cannot contain Aura. Testing: LWC supports Jest unit tests (`@salesforce/sfdx-lwc-jest`). Tooling: standard ES module imports, plus `lightning/*` base components and `@salesforce/*` imports. Some features remain Aura-only or Aura-first for specific legacy surfaces; verify current support per feature. Migration is gradual: wrap LWC inside Aura where needed.

**Code / config example:**
```html
<!-- helloWorld.html -->
<template>
    <lightning-card title="Hello">
        <template lwc:if={hasName}>
            <p class="slds-p-horizontal_small">Hello, {name}!</p>
        </template>
    </lightning-card>
</template>
```
```javascript
// helloWorld.js
import { LightningElement, api } from 'lwc';
export default class HelloWorld extends LightningElement {
    @api name; // public, set by parent
    get hasName() { return !!this.name; }
}
```

**Hidden gotchas & must-remember points:**
- Since Spring 20, class fields are reactive without `@track` (for reassignment); `@track` is only for mutating nested object or array contents.
- LWC cannot contain Aura components; the reverse works.
- Aura Locker versus LWS: behavior differs, so test third-party libraries.
- `if:true` and `if:false` are legacy; use `lwc:if`, `lwc:elseif`, `lwc:else`.
- Aura is not retired but new features land in LWC first.

**Best practice:** Build all new UI in LWC, wrap it in Aura only where a legacy surface requires, and migrate Aura components opportunistically.

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