Lightning Web Components (LWC) tutorial for beginners
On this page (10 sections)
Lesson 1: Structure of a componentLesson 2: Data: LDS, wire and ApexLesson 3: Communication between componentsLesson 4: Lifecycle, templates and stylingLesson 5: Building and deployingLesson 6: Patterns you will use every dayWhat changed in 2025–26How this is asked in interviewsPractise this topicCommon questionsLightning Web Components (LWC) is Salesforce's framework for building custom user interfaces with standard HTML, JavaScript and CSS. Use LWC when standard pages, Dynamic Forms and screen flows can't give users what they need.
In this topic: Lesson 1: Structure of a component · Lesson 2: Data: LDS, wire and Apex · Lesson 3: Communication between components · Lesson 4: Lifecycle, templates and styling · Lesson 5: Building and deploying · Lesson 6: Patterns you will use every day
Lesson 1: Structure of a component
Each component is a folder with an HTML template, a JavaScript class and a configuration file (plus optional CSS).
<!-- helloWorld.html -->
<template>
<lightning-card title="Hello">
<p class="slds-p-around_small">Hello, {name}!</p>
</lightning-card>
</template>// helloWorld.js
import { LightningElement, api } from 'lwc';
export default class HelloWorld extends LightningElement {
@api name = 'World';
}The .js-meta.xml file sets where the component can be used, for example lightning__RecordPage, lightning__AppPage, lightning__FlowScreen or lightningCommunity__Page.
Lesson 2: Data: LDS, wire and Apex
- Base components such as
lightning-record-formandlightning-record-edit-formread and save records with no Apex. - Wire adapters such as
getRecordread data reactively through Lightning Data Service, which respects security and caches results. - Apex methods marked
@AuraEnabled(cacheable=true)can be wired; others are called imperatively for saves and complex logic.
import { wire, api, LightningElement } from 'lwc';
import getContacts from '@salesforce/apex/ContactController.getContacts';
export default class ContactList extends LightningElement {
@api recordId;
@wire(getContacts, { accountId: '$recordId' }) contacts;
}Lesson 3: Communication between components
| Direction | How |
|---|---|
| Parent to child | Set the child's public @api properties, or call its @api methods |
| Child to parent | Dispatch a CustomEvent that the parent listens for |
| Unrelated components on a page | Lightning Message Service (message channels) |
Lesson 4: Lifecycle, templates and styling
- Lifecycle hooks:
connectedCallback()when added to the page,renderedCallback()after each render,disconnectedCallback()when removed. - Template directives:
lwc:if/lwc:elsefor conditions,for:eachwith akeyfor lists. - Use Salesforce Lightning Design System (SLDS) classes and base components so your component looks native and accessible.
Lesson 5: Building and deploying
- Install VS Code, the Salesforce CLI and the Salesforce Extension Pack.
- Create a project, authorise an org, and create a component with SFDX: Create Lightning Web Component.
- Deploy with SFDX: Deploy This Source to Org, then add the component to a page in Lightning App Builder.
- Write Jest tests with
@salesforce/sfdx-lwc-jestfor important logic.
Tip: Before building a component, ask whether Dynamic Forms, a screen flow or a standard component would do. The best interview answers show you choose code only when it adds value.
Lesson 6: Patterns you will use every day
These small patterns appear in almost every real component.
Change data by making a new copy
this.items = [...this.items, newItem];
this.contact = { ...this.contact, Phone: value };LWC notices when a property gets a new value. Copying with the spread operator is the safe way to change arrays and objects, and the only way to edit read-only data from @wire.
Show a toast message
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
this.dispatchEvent(new ShowToastEvent({ title: 'Saved', message: 'Contact updated', variant: 'success' }));Navigate to a record
import { NavigationMixin } from 'lightning/navigation';
export default class Card extends NavigationMixin(LightningElement) {
open() {
this[NavigationMixin.Navigate]({ type: 'standard__recordPage',
attributes: { recordId: this.recordId, actionName: 'view' } });
}
}Pick a record without Apex
<lightning-record-picker label="Account" object-api-name="Account" onchange={handlePick}></lightning-record-picker>The record picker searches records with the user's own sharing applied, so you do not need a custom Apex lookup.
Read errors the same way every time
Apex, LDS and network errors come back in different shapes. Keep one helper that pulls a readable message from error.body.message (or a list of messages) and use it in every component.
What changed in 2025–26
- Lightning Web Security (LWS) has replaced Locker Service in most orgs. It allows more third-party libraries and uses standard browser security.
lwc:if,lwc:elseifandlwc:elsereplaceif:trueandif:falsein new code.lwc:refgives a simple way to reach an element in the template, instead ofquerySelector.@trackis only needed to notice changes inside objects and arrays; plain fields are already reactive.- Dynamic components (
lwc:componentwithlwc:is) load a component only when it is needed. - New base components such as
lightning-record-picker, and the Workspace API for console apps.
How this is asked in interviews
- "Wire or imperative?" Wire for reading data that should refresh itself; imperative for actions and anything that changes data.
- "How do unrelated components talk?" Lightning Message Service. Parent and child use @api and custom events.
- "Your wired data did not refresh after a save. Why?" Cached data: call refreshApex or notifyRecordUpdateAvailable.
- "How do you make a slow component faster?" Load less data, page on the server, cache read-only Apex, debounce input. See LWC questions and LWC scenarios.
How to answer: Start with what you would do, say why, mention the limit or risk you are avoiding, and finish with a real example from a project or practice org. One clear minute beats five minutes of definitions.
Practise this topic
- LWC practice exercises, with model answers
- LWC interview questions
- LWC scenario questions
- LWC lifecycle hooks
- LWC mock interview
Common questions
01Do I need to know JavaScript to learn LWC?
Do I need to know JavaScript to learn LWC?
Yes, at least modern JavaScript basics: classes, modules, arrow functions, promises and async/await. LWC uses standard web technology.
02What is the difference between LWC and Aura?
What is the difference between LWC and Aura?
Aura is Salesforce's older framework with its own component model. LWC uses web standards, is faster and is the recommended choice for new development.
03Can LWC be used outside Lightning Experience?
Can LWC be used outside Lightning Experience?
Yes: in Experience Cloud sites, the Salesforce mobile app, flows, and in some cases outside Salesforce with Lightning Out.