LearnFrenzy
Sign in Take a free test

Salesforce LWC interview · Set 1 of 6

LWC Fundamentals: Structure, Templates and Decorators

  • 6 follow-up questions
  • 4 key points
  • Code examples
Test yourself

The building blocks of every component

Know the files, reactivity rules, decorators and template directives. Interviewers expect modern syntax (lwc:if) and clean getters instead of logic in markup.

  • StructureHTML, JS, meta XML and CSS.
  • Reactivity@api, @track, @wire and reassignment.
  • Templateslwc:if, for:each, getters and slots.

Every LWC interview starts with the basics: what a component is made of, how reactivity works and how templates render data. Get these crisp and the rest of the interview — events, data, testing — flows from them.

1. What files make up a Lightning Web Component?

A folder named after the component with an HTML template, a JavaScript class extending LightningElement, a meta XML file that controls where it can be used (record page, app page, Experience Cloud, flow screen) and exposes properties to builders, plus optional CSS and SVG files.

// orderSummary.js
import { LightningElement, api } from 'lwc';

export default class OrderSummary extends LightningElement {
    @api recordId;                       // set automatically on record pages
    @api heading = 'Order summary';      // configurable in Lightning App Builder
}

2. How does reactivity work in LWC? Do you still need @track?

All class fields are reactive: reassigning a field re-renders the template. Since Spring '20 you do not need @track for primitives or for replacing objects and arrays. @track is only needed to observe mutations inside a plain object or array (for example this.filters.city = 'Pune'). Many teams avoid it by assigning a new object instead.

filters = { city: 'All', status: 'Open' };

handleCity(event) {
    // reassign → re-renders without @track
    this.filters = { ...this.filters, city: event.detail.value };
}

3. Explain @api, @track and @wire.

@api makes a property or method public — set by the parent or the App Builder; the child must not mutate it. @track deep-observes object and array mutations. @wire connects a property or function to a data source (an LDS adapter or an @AuraEnabled(cacheable=true) Apex method) that streams data reactively.


4. How do you render conditionally and loop in templates?

Use lwc:if, lwc:elseif and lwc:else (the modern replacement for if:true/if:false). Loop with for:each and a unique key, or iterator when you need first/last flags. Templates allow only property references — no expressions — so compute values in getters.

<template>
    <template lwc:if={isLoading}>
        <lightning-spinner alternative-text="Loading"></lightning-spinner>
    </template>
    <template lwc:elseif={hasOrders}>
        <template for:each={orders} for:item="order">
            <p key={order.Id}>{order.Name} — {order.Status__c}</p>
        </template>
    </template>
    <template lwc:else>
        <p>No orders yet.</p>
    </template>
</template>

5. Why use getters in LWC?

Templates cannot evaluate expressions such as {orders.length > 0}. A getter computes the value from current state each render — clean, testable and always in sync.

get hasOrders() {
    return Array.isArray(this.orders) && this.orders.length > 0;
}
get totalLabel() {
    const total = this.orders.reduce((sum, o) => sum + (o.Amount__c || 0), 0);
    return `Total: ₹${total.toLocaleString('en-IN')}`;
}

6. How does data binding work between input and component?

LWC has one-way data binding. Values flow into the template from properties; changes come back through event handlers you write. Lightning base components fire change events with the value in event.detail.value or event.target.value.

<lightning-input label="Search" value={searchTerm} onchange={handleSearch}></lightning-input>

// JS
searchTerm = '';
handleSearch(event) {
    this.searchTerm = event.target.value;
}

7. What are slots?

Slots let a parent pass markup into a child. An unnamed <slot> receives default content; named slots (<slot name="footer">) receive content marked with slot="footer". Useful for reusable cards, modals and layouts.

<!-- c-card-shell -->
<template>
    <article class="slds-card">
        <header><slot name="title"></slot></header>
        <div class="slds-card__body"><slot></slot></div>
        <footer><slot name="footer"></slot></footer>
    </article>
</template>

<!-- usage -->
<c-card-shell>
    <h2 slot="title">Open cases</h2>
    <c-case-list></c-case-list>
    <lightning-button slot="footer" label="View all"></lightning-button>
</c-card-shell>

8. How is LWC different from Aura?

LWC is built on web standards (custom elements, shadow DOM, ES modules) and runs much of its work natively in the browser, so it is faster, lighter and closer to modern JavaScript. Aura is Salesforce's older proprietary framework. LWC and Aura interoperate — LWC can sit inside Aura — but new development should be LWC.


Remember

Key points

  • Fields are reactive

    @track only for inner mutations.

  • Never mutate @api props

    Copy them into a private field.

  • Logic in getters

    Templates hold no expressions.

  • Use lwc:if

    if:true is the legacy syntax.

Interview drill

Follow-up questions

What the interviewer asks after your first answer. Turn on practice mode, answer each one aloud, then reveal.

  1. 1 What is the shadow DOM in LWC? Intermediate

    Each component's markup and styles are encapsulated, so CSS does not leak in or out and parents cannot query a child's internal elements. Salesforce also offers light DOM for special cases.

  2. 2 How do you use a property from the meta XML in App Builder? Basic

    Declare it with @api in JS and add a <property> under the target config in the meta XML.

  3. 3 What is the iterator directive used for? Basic

    It exposes first, last, index and value for each item, for styling the first or last row differently.

  4. 4 How do you apply SLDS styling? Basic

    Use Lightning base components and SLDS utility classes; custom CSS in the component file is scoped by the shadow DOM. Use styling hooks (CSS custom properties) to theme base components.

  5. 5 What is lwc:ref? Intermediate

    A directive to reference an element in the template from JS via this.refs.name — cleaner than querySelector.

  6. 6 Can you use third-party JavaScript libraries? Intermediate

    Yes — upload them as static resources and load with loadScript/loadStyle from lightning/platformResourceLoader, usually in renderedCallback once.

Preparing with friends?

Share this page with your study group or placement batch.

Search LearnFrenzy

Esc

Quick links