Testing and Interview Prep · Lesson 25 of 25
LWC Interview Questions
Salesforce developer interviews test LWC from two angles: do you know the framework's rules (decorators, lifecycle, reactivity), and can you make good design decisions (LDS or Apex, events or LMS, wire or imperative)? These are the questions that come up most, with concise answers – several backed by code you can see running in this course.
In this lesson
- Fundamentals and decorators
- Reactivity and lifecycle
- Communication and data
- Security, performance and testing
Fundamentals
1. What is LWC, and why did Salesforce introduce it over Aura?
A UI framework built on web standards (custom elements, shadow DOM, ES modules), with a thin layer for reactivity and Salesforce services. Compared with Aura it's faster, uses standard JavaScript, and is easier to learn. Aura can contain LWC; LWC can't contain Aura.
2. What are the three decorators?
| Decorator | Purpose |
|---|---|
@api | public property or method – set by a parent, App Builder, Flow or the record page |
@wire | connects a property or function to a wire adapter (LDS, Apex, MessageContext…) |
@track | observes in-place changes inside a plain object or array; rarely needed now |
3. What files make up a component, and which are required?
HTML template, JavaScript class and js-meta.xml are required to deploy a UI component; CSS, extra JS modules and Jest tests are optional. Folder names are camelCase; markup uses c- plus kebab-case.
Reactivity and lifecycle
4. Why doesn't this component update when the button is clicked?
<!-- skillList.html -->
<template>
<p>Skills: {skillText}</p>
<lightning-button label="Add Apex" onclick={addSkill}></lightning-button>
</template>
// skillList.js
import { LightningElement } from 'lwc';
export default class SkillList extends LightningElement {
skills = ['LWC'];
get skillText() {
return this.skills.join(', ');
}
addSkill() {
this.skills.push('Apex');
}
}
Skills: LWC [Add Apex] › click “Add Apex” Skills: LWC [Add Apex]
Fields are tracked shallowly: push mutates the same array, so LWC sees no new value. Fix: this.skills = [...this.skills, 'Apex'], or decorate the field with @track.
5. What is the order of lifecycle hooks for a parent with a child?
Parent constructor → parent connectedCallback → child constructor → child connectedCallback → child renderedCallback → parent renderedCallback. disconnectedCallback runs on removal; errorCallback in an ancestor catches errors from descendants.
6. Where do you load data, subscribe to LMS, and work with the DOM?
Imperative loads and subscriptions in connectedCallback (unsubscribe in disconnectedCallback); DOM work in renderedCallback with a guard flag; never set reactive fields unconditionally in renderedCallback.
7. lwc:if vs if:true?
if:true/if:false are deprecated. lwc:if, lwc:elseif and lwc:else form one chain, are more efficient and read like JavaScript.
Communication and data
8. How do components communicate?
Parent → child with @api properties and methods (lwc:ref); child → parent with CustomEvent; unrelated components with Lightning Message Service; components showing the same record stay in sync through LDS automatically.
9. What do bubbles and composed do on a custom event?
By default an event reaches only the component's host (its parent's listener). bubbles: true lets it bubble through the parent's template; adding composed: true lets it cross shadow boundaries to the document. Avoid composed unless the event is meant to be public.
10. LDS or Apex?
LDS (getRecord, record forms, updateRecord) for single records: built-in cache, sync and security. Apex for lists with filters, aggregates, complex logic, callouts and bulk changes. Apex data isn't synced: use refreshApex and notifyRecordUpdateAvailable.
11. @wire vs imperative Apex?
Wire for read-only, cacheable=true data that should load automatically and re-load when reactive '$' parameters change. Imperative for user-triggered calls, DML, callouts and when you control timing or sequencing.
12. Why can't you modify data returned by @wire?
Wired data is frozen (read-only) so the shared cache can't be corrupted. Copy it: data.map((r) => ({ ...r })).
13. How do you refresh wired Apex data after a save?
Store the whole wired result and call refreshApex(this.wiredResult). For records changed in Apex that LDS components show, call notifyRecordUpdateAvailable([{ recordId }]).
Security, performance and testing
14. What is Lightning Web Security?
The runtime that isolates each namespace's JavaScript in its own sandbox and restricts dangerous APIs; it replaced Lightning Locker. Components can only access their own DOM.
15. Is hiding a button in LWC enough to protect an action?
No. Any user with access to an @AuraEnabled class can call its methods directly. Enforce with sharing, WITH USER_MODE / as user DML and explicit permission checks in Apex.
16. How do you make a component fast?
Use LDS and cacheable Apex; query only needed fields with LIMIT; paginate; debounce input-driven calls; keep getters cheap and precompute when data arrives; use stable keys; lazy-load libraries and rarely-used children.
17. How do you test LWC?
With Jest and @salesforce/sfdx-lwc-jest: create the element, emit wire data or mock Apex with jest.mock, simulate clicks, await, and assert on the shadow DOM, mocks and events.
18. How do you use LWC in a Screen Flow?
Target lightning__FlowScreen, expose inputs and outputs as @api properties in targetConfigs, dispatch FlowAttributeChangeEvent to update outputs, and implement @api validate().
How to stand out
- Explain the why: shallow reactivity, frozen wire data, events up / data down, LDS caching.
- Mention current syntax and APIs:
lwc:if,lwc:ref,LightningModal,sfCLI, Lightning Web Security. - Always bring up security in Apex and testing with Jest – many candidates forget both.
Practise further with our LWC interview question sets, the Apex interview questions, and the JavaScript for LWC track.
Practice
Try it yourself
Without running it, predict what this child logs when its parent changes
namefrom "Asha" to "Ravi".// greeter.js import { LightningElement, api } from 'lwc'; export default class Greeter extends LightningElement { _name; @api get name() { return this._name; } set name(value) { console.log(`setter: ${value}`); this._name = value; } connectedCallback() { console.log(`connected with ${this._name}`); } renderedCallback() { console.log(`rendered ${this._name}`); } }<!-- greeter.html --> <template> <p>Hi {name}</p> </template>Show answer
Hi Asha console: setter: Asha console: connected with Asha console: rendered Asha › parent sets name = "Ravi" Hi Ravi console: setter: Ravi console: rendered Ravi
The setter runs first (before connectedCallback, because the parent sets properties right after creating the element), then connectedCallback and the first render. Changing the property runs the setter again and re-renders – but connectedCallback doesn't run again.
Quick check
Which decorator exposes a method to a parent?
@api.
What must an Apex method have to be used with @wire?
@AuraEnabled(cacheable=true).
Which communication method works between LWC and Visualforce on one page?
Lightning Message Service.
Summary
- Know the rules: decorators, shallow reactivity, lifecycle order, lwc:if.
- Make design choices out loud: LDS vs Apex, wire vs imperative, events vs LMS.
- Show production thinking: Apex security, performance and Jest tests.
Finished this lesson?
Mark it complete to track your progress. You have reached the end of this track — well done!