When you need many objects with the same shape, such as lots of customers, enemies in a game or products in a shop, writing each object by hand gets repetitive. A class is a blueprint: you describe the shape and behaviour once, then create as many objects, called instances, as you need.
Classes are the main tool for a style of programming called object-oriented programming (OOP), where data and the functions that work on it are kept together. You'll meet classes in many libraries and in other languages such as Python and Java, so the ideas here carry over well beyond JavaScript.
Defining and using a class
class Pet {
constructor(name, species) {
this.name = name;
this.species = species;
}
describe() {
return `${this.name} is a ${this.species}`;
}
}
const pip = new Pet("Pip", "parrot");
pip.describe(); // "Pip is a parrot"- Class names use PascalCase, as in
PetorShoppingCart. constructorruns automatically when you callnew Pet(…). Use it to set up the new object.thisrefers to the instance being created or used.- Methods are written without the
functionkeyword, and all instances share them.
Forgetting new is a common mistake. Calling Pet("Pip", "parrot") without it throws an error.
Getters and static methods
get total() { … }defines a computed property that you read like a normal property, ascart.total, without brackets.static create() { … }defines a method that belongs to the class itself rather than to instances, so you call it asPet.create().
Getters are handy for values that depend on other data, such as a basket total that should always reflect the current items.
Inheritance with extends and super
class Dog extends Pet {
constructor(name) {
super(name, "dog"); // call the parent's constructor first
}
bark() { return "Woof!"; }
}Dog gets everything Pet has, plus its own extras. A child class can also override a parent's method by defining a method with the same name.
Use inheritance for genuine "is a" relationships: a dog is a pet. Keep inheritance chains short; long chains of classes become hard to follow.
Private fields
Fields whose names start with # can only be used inside the class:
class BankAccount {
#balance = 0;
deposit(n) { this.#balance += n; }
get balance() { return this.#balance; }
}Outside code can read account.balance through the getter but can't change #balance directly. That protects the object from being put into an invalid state, such as a negative balance set by mistake.
JavaScript Classes Syntax
Class syntax
class Name {
#secret = 0; // private field
constructor(a) { this.a = a; }
method() { return this.a; }
get value() { return this.#secret; }
static helper() { }
}
class Child extends Name {
constructor(a) { super(a); }
}
const obj = new Name(1);JavaScript Classes Summary
- A class is a blueprint for creating many similar objects.
constructorsets up each new instance, created withnew.- Methods are shared by all instances, and
thisrefers to the current instance. extendsandsuperlet one class build on another.#privatefields can only be accessed inside the class.
JavaScript Classes Examples
Each example runs in the page. Edit the code and press Run to see what changes.
A simple class
class Timer {
constructor(label, seconds) {
this.label = label;
this.seconds = seconds;
}
tick() {
this.seconds--;
return this.seconds;
}
get display() {
const m = Math.floor(this.seconds / 60);
const s = String(this.seconds % 60).padStart(2, "0");
return `${this.label}: ${m}:${s}`;
}
}
const tea = new Timer("Tea", 180);
const egg = new Timer("Egg", 361);
tea.tick();
console.log(tea.display);
console.log(egg.display);Line-by-line explanation
- Lines 2 to 5The constructor stores each timer's own data.
- Lines 7 to 10A method that changes this instance.
- Lines 12 to 16A getter, read like a property.
- Lines 19 to 20Two separate instances from one blueprint.
Inheritance
class Character {
constructor(name, hp) {
this.name = name;
this.hp = hp;
}
attack() {
return `${this.name} attacks for 5 damage`;
}
}
class Wizard extends Character {
constructor(name) {
super(name, 60);
this.mana = 100;
}
attack() {
this.mana -= 20;
return `${this.name} casts a fireball for 20 damage (mana ${this.mana})`;
}
}
const knight = new Character("Sir Bean", 100);
const wiz = new Wizard("Merla");
console.log(knight.attack());
console.log(wiz.attack());
console.log(wiz.hp, wiz instanceof Character);Line-by-line explanation
- Line 11Wizard inherits from Character.
- Line 13
super(…)runs the parent constructor, which is required before usingthis. - Lines 16 to 19Override
attackwith a wizard version. - Line 27Wizards are also Characters.
Private fields and static methods
class Cart {
#items = [];
add(name, price) {
this.#items.push({ name, price });
return this;
}
get total() {
return this.#items.reduce((sum, i) => sum + i.price, 0);
}
static fromList(list) {
const cart = new Cart();
list.forEach(([n, p]) => cart.add(n, p));
return cart;
}
}
const cart = new Cart().add("Socks", 6).add("Hat", 12);
console.log("Total:", cart.total);
console.log(cart.items);
const quick = Cart.fromList([["Pen", 2], ["Pad", 3]]);
console.log("Quick total:", quick.total);Line-by-line explanation
- Line 2A private array. Outside code can't touch it.
- Line 6Returning
thislets you chain.add().add(). - Lines 9 to 11A getter computes the total.
- Lines 13 to 17A static "factory" method on the class itself.
- Line 22The private field is invisible from outside:
undefined.
Common JavaScript Classes Mistakes
Forgetting new
const p = Pet("Pip", "parrot"); // TypeError: Class constructor cannot be invoked without 'new'const p = new Pet("Pip", "parrot");Using this before super
class Dog extends Pet {
constructor(name) {
this.tricks = []; // ReferenceError
super(name, "dog");
}
}class Dog extends Pet {
constructor(name) {
super(name, "dog");
this.tricks = [];
}
}Commas between methods
class A {
one() {},
two() {}
}class A {
one() {}
two() {}
}Unlike object literals, class bodies don't use commas.
JavaScript Classes in Real Projects
Classes in the real world
Browsers use classes everywhere (every element is an instance of a class like HTMLButtonElement). Game engines model players and enemies with classes, Node.js servers use them for services, and Web Components are built by extending HTMLElement.
JavaScript Classes Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
Complete the class.
class Book {
(title) {
.title = title;
}
}
const b = Book("Dune");
constructor sets up the instance via this; new creates one.What does super(name) do inside a child class constructor?
this in a child constructor.What does this print?
class Counter {
#n = 0;
inc() { this.#n++; return this.#n; }
}
const c = new Counter();
c.inc(); c.inc();
console.log(c.inc(), c.n);#n is private, so c.n is a different, non-existent property.new Square(4).area should be 16, and Square should extend Shape. Fix it.
class Shape {
constructor(name) {
this.name = name;
}
}
class Square extends Shape {
constructor(side) {
this.side = side;
super("square");
}
get area() {
return side * side;
}
}
console.log(new Square(4).area);- Area of a 4-sided square is 16
- Square has the name "square"
- Square is a Shape
JavaScript Classes Quiz
3 questions to check what you learned.
What naming style do classes use?
- PascalCase
- camelCase
- snake_case
- kebab-case
When does the constructor run?
- When the class is defined
- Each time you create an instance with
new - When a method is called
- Never automatically
What makes a field private in a class?
- The
privatekeyword - An underscore
_balance - A
#prefix, like#balance - Declaring it in the constructor
- The
JavaScript Classes Challenge
Build a Playlist class
Create class Playlist with:
- A constructor taking
name, starting with an emptysongsarray add(title, seconds): pushes{ title, seconds }and returnsthis- A getter
durationreturning the total seconds - A method
summary()returning"Road Trip: 2 songs, 7 min"(minutes rounded down)
const trip = new Playlist("Road Trip");
trip.add("Song A", 200).add("Song B", 250);
console.log(trip.summary());- Playlist is a class
- add is chainable
- duration totals the seconds
- summary is correct
- Logs the road trip summary