JavaScript classes tutorial (constructor, methods, static, extends)

JavaScript classes: constructor, instance methods, getters, static methods, extends and super, public fields, and private # fields. Runnable Node snippets with short expected-output notes; links to ranges and cloning patterns.

Published

Updated

Read time 5 min read

Reviewed byDeepak Prasad

JavaScript classes tutorial (constructor, methods, static, extends)

This javascript classes tutorial walks through a practical javascript classes example: class, optional constructor, javascript classes methods, getters, static, extends / super, public fields, and # private fields—topics people look up as class javascript example, classes javascript example, or js class tutorial. The same Array.from patterns appear when building numeric ranges, and instance data often flows through object cloning pipelines.

Tested on: Node.js v20.18.2. A short note after each snippet describes what you should see in the console.


Quick reference

Instance API goes on the prototype; static belongs to the constructor; # is lexically private—pick the row before you reach for a closure or _prefix convention.

Need Pattern
Shared behavior per instance Instance methods on the class body
Value derived from state get prop() / set prop(v)
Utility on the type static method()
Hide internal state #privateField
Specialize a type extends + super()

Defining classes in JavaScript — minimal class and new

An empty class body is valid defining classes in javascript syntax: you still instantiate with new, and instanceof confirms the relationship. If you omit constructor, the engine provides a default empty one (MDN: constructor).

javascript
class Empty {}
console.log(new Empty() instanceof Empty);
Output

You should see one line: true.


Constructor and instance state — classes in javascript example

The constructor runs when you call new: use it to attach per-instance fields such as name and radius on each Planet. This is the standard classes in javascript example for modeling simple records before you add methods.

javascript
class Planet {
  constructor(name, radius) {
    this.name = name;
    this.radius = radius;
  }
}
const p = new Planet("Earth", 6378);
console.log(p.name, p.radius);
Output

You should see one line: Earth 6378.


Instance methods — javascript classes methods

Methods declared in the class body become shared behavior on the prototype: each instance uses the same function identity while this binds to the receiver. This section is the core javascript classes methods pattern for computed values like surfaceArea().

javascript
class Planet {
  constructor(name, radius) {
    this.name = name;
    this.radius = radius;
  }
  surfaceArea() {
    return 4 * Math.PI * this.radius * this.radius;
  }
}
const earth = new Planet("Earth", 6378);
console.log(earth.name, Math.round(earth.surfaceArea()));
Output

You should see one line like: Earth 511185933 (rounded surface area).


Getter — using classes in javascript

A getter lets consumers read r.area like a field while you derive the value from w and h—common in using classes in javascript when you want a stable API without parentheses (MDN: getter).

javascript
class Rect {
  constructor(w, h) {
    this.w = w;
    this.h = h;
  }
  get area() {
    return this.w * this.h;
  }
}
const r = new Rect(4, 5);
console.log(r.area);
Output

You should see one line: 20.


Private fields (#) — encapsulation in modern javascript classes tutorial

The # prefix marks truly private instance state: not visible on this from outside the class, unlike old _convention patterns. It answers classes javascript example questions about hiding counters or tokens without a closure factory.

javascript
class Counter {
  #n = 0;
  inc() {
    this.#n++;
    return this.#n;
  }
}
const c = new Counter();
console.log(c.inc(), c.inc());
Output

You should see one line: 1 2.


Static methods — on the class, not the instance

static methods live on the constructor (User.parse, MathUtil.clamp): they are the right js class example for factories and pure helpers that do not need an instance this (MDN: static).

javascript
class MathUtil {
  static clamp(v, lo, hi) {
    return Math.min(hi, Math.max(lo, v));
  }
}

class User {
  constructor(login) {
    this.login = login;
  }
  static parse(line) {
    const [login] = line.split(":");
    return new User(login);
  }
}

console.log(MathUtil.clamp(15, 0, 10));
console.log(User.parse("alice:admin").login);
Output

You should see two lines: 10, then alice.


extends and super — inheritance (class extends javascript)

Subclassing with extends chains prototypes; in the subclass constructor you must call super(...) before using this, then you can override methods and still call the parent implementation via super.method() (MDN: extends, MDN: super).

javascript
class Animal {
  constructor(name) {
    this.name = name;
  }
  speak() {
    return this.name;
  }
}
class Dog extends Animal {
  constructor(name) {
    super(name);
  }
  speak() {
    return super.speak() + ": woof";
  }
}
const d = new Dog("Rex");
console.log(d.speak());
Output

You should see one line: Rex: woof.


Public class fields

Public fields initialize instance properties before the constructor body runs—handy defaults for javascript classes example code without boilerplate assignments (MDN: Public class fields).

javascript
class Box {
  label = "todo";
}
const b = new Box();
console.log(b.label);
Output

You should see one line: todo.


Summary

class gives you new, constructor, shared methods, static, extends/super, # privacy, and public fields—match the feature to whether the logic needs an instance or the type itself.

  • Use a minimal class + new when defining classes in javascript with no setup logic; add constructor for parameters and instance setup.
  • Put shared behavior in instance methods; use getters when a property should read like a field but derive from state.
  • Use static for factories and utilities; use # fields for encapsulation in modern javascript classes tutorial style code.
  • Subclass with extends and call super() before this in the child constructor; override methods and delegate with super.method() for class extends javascript patterns.
  • Public fields give concise defaults on each instance without extra constructor lines.

References

MDN guides for classes, constructors, static members, inheritance, privacy, and public fields—canonical docs for this javascript classes tutorial.


Frequently Asked Questions

1. Is a constructor required when defining classes in javascript?

No. If you omit it, the class gets an empty default constructor. You only write constructor when you need to initialize instance state or accept parameters.

2. Are class declarations hoisted like function declarations?

Class declarations are in the temporal dead zone until evaluated; you cannot use the class name before its declaration line in the same scope. Function declarations are hoisted differently.

3. javascript classes methods vs static methods?

Instance methods live on the prototype and use this on an instance. Static methods belong to the constructor function (the class object itself) and are called as ClassName.method().

4. When must I call super() in a subclass?

In a derived class constructor, call super(...) before using this, per ECMAScript rules. super(...) invokes the parent constructor.

5. What does the

It marks private fields or private methods. They are only accessible inside the class body, not from outside code or subclasses (for private instance features).

6. javascript classes example without new?

Calling a class like a normal function throws TypeError in strict class mode. Always instantiate with new ClassName() unless the class explicitly supports being called without new (unusual).
Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs …