Skip to main content

Building Your First Salesforce Lightning Web Component (LWC): A Beginner's Guide

In plain words: Salesforce Lightning Web Components (LWC) is the modern framework used to build custom user interfaces on the Salesforce platform. Instead of using older, outdated technologies like Aura, LWC relies on standard, lightweight web technologies—HTML, CSS, and modern JavaScript—making your Salesforce apps run faster and load smoother.

If you are new to Salesforce frontend development, building your very first Lightning Web Component is an exciting milestone. LWC gives developers the power to construct responsive, high-performance web applications that integrate seamlessly with Salesforce data.

In this guide, we will walk through the exact steps required to create a simple "Hello World" component complete with HTML markup, custom styling, and reactive JavaScript logic.

Prerequisites

  • A Salesforce Developer Edition account or sandbox.
  • Basic familiarity with HTML, CSS, and JavaScript.

Step 1: Create a New Lightning Web Component

To begin, log in to your Salesforce account and open the Developer Console. From the top menu, select File > New > Lightning Web Component, name your component helloWorld, and submit.

Step 2: Write the HTML Markup

Open your new helloWorld.html file and replace the default template code with the following structure:

<template>
    <div class="container">
        <h1>Hello, Lightning Web Components!</h1>
        <lightning-button label="Click Me" onclick={handleClick}></lightning-button>
        <p>{greeting}</p>
    </div>
</template>

Step 3: Add CSS Styling

LWC automatically scopes CSS stylesheets to individual components, meaning your styles won't accidentally leak out and break other parts of Salesforce. Create a stylesheet named helloWorld.css and add the following design rules:

.container {
    text-align: center;
    padding: 20px;
    background-color: #f0f0f0;
    border-radius: 8px;
}

h1 {
    font-size: 24px;
    margin-bottom: 20px;
    color: #1f4e79;
}

p {
    font-size: 18px;
    font-weight: bold;
    color: #262626;
    margin-top: 15px;
}

Step 4: Implement the JavaScript Controller

Open your helloWorld.js file. This controller handles user events and manages the component's reactive state.

import { LightningElement, track } from 'lwc';

export default class HelloWorld extends LightningElement {
    // Reactive property to store the greeting message
    @track greeting = '';

    handleClick() {
        this.greeting = 'Hello, you clicked the button!';
    }
}
Developer Trap: The @track Decorator
While older versions of LWC required the @track decorator for almost all reactive variables, modern LWC automatically tracks primitive variables (like strings, booleans, and numbers) declared in your class. You only need @track today if you are tracking deep mutations inside objects or arrays.
Step-by-Step Data Binding:
  1. The user clicks the <lightning-button>.
  2. The button's onclick={handleClick} attribute triggers the JavaScript function.
  3. The function updates this.greeting with a new string.
  4. Because greeting is bound in the HTML template via {greeting}, the browser re-renders the paragraph tag instantly.
360 Card: LWC File Structure Checklist
  • HTML (.html): The user interface template.
  • JavaScript (.js): The controller logic and event handlers.
  • CSS (.css): Component-scoped styling.
  • Configuration (.js-meta.xml): Defines where and how the component can be exposed in Salesforce Lightning Experience.
Core Takeaway: Building an LWC involves pairing a clean HTML template with an event-driven JavaScript controller and scoped CSS styling, all wrapped inside a standardized Salesforce bundle.

Conclusion

Congratulations! You have successfully built, styled, and tested your very first Lightning Web Component. While this example is simple, it covers the foundational concepts—event handling, data binding, and component structure—that you will use when building complex enterprise applications on the Salesforce platform.

Happy coding!