Beginner 18 min read

Functions, Arrow Functions, and Classes

By the end of this lesson, you'll be able to:

  • Write and use arrow functions
  • Explain how arrow functions handle "this" differently from regular functions
  • Recognize the ES6 class syntax every LWC component is built from

Prerequisites: "Arrays, Array Methods, and the Spread Operator"

Regular Functions vs. Arrow Functions

function double(n) {
    return n * 2;
}

const doubleArrow = (n) => n * 2;

Arrow functions are shorter, and — critically — they don't have their own this. Instead, they inherit this from the surrounding scope, which is usually exactly the behavior you want when passing a short callback into an array method like .map() or .filter().

Classes in JavaScript

class Animal {
    constructor(name) {
        this.name = name;
    }

    speak() {
        return `${this.name} makes a sound.`;
    }
}

This class ... constructor ... method shape is exactly the shape of every LWC component you write:

export default class MyComponent extends LightningElement {
    // fields and methods, same shape as any other JS class
}

extends LightningElement means your component is a class, inheriting everything a Lightning Web Component needs from that base class.

A Worked Example

class CaseFormatter {
    constructor(caseRecord) {
        this.caseRecord = caseRecord;
    }

    get displayLabel() {
        return `Case #${this.caseRecord.CaseNumber}`;
    }
}

Notice the get keyword — this is the exact same getter syntax used inside LWC components (Module 3), because it's plain JavaScript class syntax, not something LWC invented.

Why This Matters in Real Projects

Understanding classes isn't a side topic for LWC — it's directly understanding the shape of every single component file you'll write for the rest of this course, and the reason concepts like getters and lifecycle methods (Module 3) feel consistent across every component.

Exercise

Rewrite this regular function as an arrow function: function square(n) { return n * n; }

Show hint

Arrow functions can often be written on one line for simple expressions.

JAVASCRIPT

Exercise

Challenge: explain, as a comment, what "export default class MyComponent extends LightningElement" actually means, in terms of plain JavaScript class syntax.

Show hint

Break down "export default", "class ... extends", and what inheritance provides.

JAVASCRIPT

Functions, Arrow Functions, and Classes Quiz

1. What is a key difference between arrow functions and regular functions?

2. What does "extends LightningElement" mean for an LWC component class?

3. Every Lightning Web Component is fundamentally a plain JavaScript ES6 class.

4. Where else in this course have you already seen the "get" keyword used, in the exact same syntax as a plain JS class?

5. What does a class constructor do?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

Every single Lightning Web Component you write is, at its core, an ES6 class — understanding classes and arrow functions is understanding the literal shape of LWC code.