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).
class Empty {}
console.log(new Empty() instanceof Empty);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.
class Planet {
constructor(name, radius) {
this.name = name;
this.radius = radius;
}
}
const p = new Planet("Earth", 6378);
console.log(p.name, p.radius);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().
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()));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).
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);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.
class Counter {
#n = 0;
inc() {
this.#n++;
return this.#n;
}
}
const c = new Counter();
console.log(c.inc(), c.inc());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).
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);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).
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());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).
class Box {
label = "todo";
}
const b = new Box();
console.log(b.label);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+newwhen defining classes in javascript with no setup logic; addconstructorfor 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
staticfor factories and utilities; use#fields for encapsulation in modern javascript classes tutorial style code. - Subclass with
extendsand callsuper()beforethisin the childconstructor; override methods and delegate withsuper.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.
- MDN: Classes
- MDN: Using classes
- MDN:
constructor - MDN:
static - MDN:
extends - MDN: Private class features
