Skip to content

JavaScript Classes

ES6 class syntax for constructors, methods, inheritance, static members, and private fields.

What you'll learn

  • Declaring classes with constructors and methods
  • Inheritance with extends and super
  • Static methods and properties on the class itself
  • Private fields with the # prefix (ES2022)
  • Getters and setters with get and set

Concept

Class Syntax

ES6 class syntax is a cleaner way to write constructor functions and prototype methods. A class declaration creates a function (the constructor) with methods attached to its prototype. Under the hood, the prototype system is unchanged — classes are syntactic sugar that makes the code more readable and familiar to developers from other languages.

Constructor and Methods

The constructor is a special method that runs when you create an instance with new. It initializes the instance's own properties. Other methods defined in the class body are placed on the prototype and shared by all instances. You can also define methods outside the constructor for behavior.

Inheritance with extends and super

class Child extends Parent sets up the prototype chain so the child inherits the parent's methods. The child's constructor must call super(...) before using this. super.method() calls the parent's version of a method, enabling override-and-extend patterns. This mirrors classical inheritance while remaining prototypal underneath.

Static Members

static methods and properties belong to the class itself, not to instances. They are called on the class (ClassName.method()), useful for utility functions and factory methods. Static methods cannot access this of an instance — they receive the class as this when called on the class.

Private Fields and Accessors

ES2022 introduced truly private fields with the # prefix. A #balance field cannot be read or written from outside the class, giving real encapsulation without closures. The get and set keywords define accessor properties that run a function when a property is read or assigned — useful for validation and computed properties.

Example

              class Animal {
  #name; // private field — inaccessible outside the class

  constructor(name) {
    this.#name = name;
  }

  get name() {
    return this.#name;
  }

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

  static create(name) {
    return new Animal(name);
  }
}

class Dog extends Animal {
  #breed;

  constructor(name, breed) {
    super(name); // must call super before using 'this'
    this.#breed = breed;
  }

  speak() {
    return `${this.name} barks`; // override and extend
  }

  get info() {
    return `${this.name} (${this.#breed})`;
  }
}

const rex = new Dog("Rex", "Labrador");
console.log(rex.speak()); // "Rex barks"
console.log(rex.info);   // "Rex (Labrador)" — getter
// console.log(rex.#name); // SyntaxError — private

// Static method called on the class, not an instance
const generic = Animal.create("Creature");
console.log(generic.speak()); // "Creature makes a sound"

// instanceof checks the inheritance chain
console.log(rex instanceof Dog);   // true
console.log(rex instanceof Animal); // true
            

Shows a base class with a private field and getter, a subclass using extends and super, method overriding, a static factory method, a getter accessor, and instanceof checks across the chain.

Try it

  • JSON Formatter

    Class instances serialize to JSON — inspect their structure.

  • JSON Tree Viewer

    Browse the properties of objects created from classes.

Common mistakes

The mistake

Forgetting to call super() in a subclass constructor before using this

The fix

In a subclass, you must call super(...) before accessing this. JavaScript throws a ReferenceError otherwise. Call super first, then set instance properties.

The mistake

Trying to access a private (#) field from outside the class

The fix

Private fields are truly private — they cannot be read or written outside the class body, even by subclasses. Expose them through a getter or a public method if external access is needed.

Related Cheatsheets

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors