Skip to main content

LWC Code Commenting: Best Practices & Examples for Clean Code

In plain words: Code commenting in Lightning Web Components (LWC) is the practice of leaving clear, helpful notes inside your JavaScript, HTML, and CSS files. Effective comments explain why a piece of code exists, rather than just what it does, ensuring your Salesforce applications stay easy to read, maintain, and scale for your whole team.

Writing great code isn't just about making things work—it's about making sure your team (and future you) can easily understand and maintain it. In the world of Salesforce development, taking a few extra seconds to properly comment your Lightning Web Components can save hours of debugging down the road.

Let's dive into the core best practices for formatting and writing comments across your LWC architecture.

1. General Commenting Guidelines

When adding notes to your LWC JavaScript files, clarity is your best friend. Follow these foundational rules:

  • Be descriptive but concise: Share valuable context about the code's purpose and highlight any potential "gotchas."
  • Don't restate the obvious: Avoid explaining exactly what basic JavaScript methods do. Focus on the business logic instead.
  • Explain workarounds: If you had to write a hacky solution because of a platform limitation, explain exactly why that workaround was necessary.
Example: Explaining Business Logic
Here is how you might comment a standard @wire adapter fetching account data.
// Fetches account records from the Apex controller and binds them to the UI.
@wire(getAccounts)
fetchAccounts({ error, data }) {
  if (data) {
    this.accounts = data;
  } else if (error) {
    this.error = error;
  }
}
Developer Trap: Outdated Comments
Never forget to update your comments when you change your code. A comment that describes old logic is incredibly misleading and often causes more confusion than having no comment at all!

2. Commenting Component Files (JSDoc Style)

For your core JavaScript files, adopting the JSDoc standard is a highly recommended practice in the Salesforce community. It provides a structured way to document your files and methods.

File-Level Comments: At the very top of your .js file, include a brief description of what the component does, the Apex controllers it depends on, and authorship details.

/**
 * @description A custom Lightning Web Component that displays a filtered list of accounts.
 * Dependencies: getAccounts Apex method.
 * Author: Jane Doe
 * Created: August 17, 2026
 */

Method-Level Comments: Before declaring complex functions, describe the expected behavior, the parameters it requires, and what it returns.

/**
 * @description Processes the raw account data and applies the user's active filters.
 * @param {Object} error - An error object returned from the server, if any.
 * @param {Array} data - The array of retrieved account records.
 */
@wire(getAccounts)
fetchAccounts({ error, data }) {
  // Logic goes here...
}

3. Commenting HTML Markup and CSS

LWC isn't just JavaScript. Your component bundles include HTML templates and CSS files that also require documentation.

  • Markup Comments: Use HTML comments to break up large template files, describe the purpose of specific UI wrappers, or explain conditional rendering tags like lwc:if or for:each.
  • CSS Comments: Use CSS comments to explain complex layout rules, specific z-index choices, or reasons behind overriding standard Salesforce Lightning Design System (SLDS) tokens.
<!-- HTML: Displayed only when the user has no active accounts -->
<div class="empty-state">
  <p>No accounts found for this filter.</p>
</div>
/* CSS: Overriding standard background to match the company's branding guidelines */
.highlighted-record {
  background-color: var(--lwc-colorBackgroundHighlight, #fff2cc);
}
Core Takeaway: Write code for computers, but write comments for humans.

Final Thoughts

Implementing a solid code commenting strategy in your Lightning Web Components drastically improves collaboration across your Salesforce development team. By using clear descriptions, adopting JSDoc standards for methods, and ensuring your comments evolve alongside your codebase, you set your projects up for long-term success and easier maintenance.

Happy coding!