A class is a template for an object. The constructor method is called automatically when an instance is created via new, and this in its body refers to the object being created. Public fields are declared directly in the class body and get a default value.
- Group data and behavior together in one object.
- Create many similar objects from one template.
class User {
constructor(name) {
this.name = name
}
}
const u = new User("Anna")
console.log(u.name)Output:
Annanew User("Anna") calls the constructor, this.name stores "Anna", and it is accessed with a dot.
class Circle {
radius = 1
}
console.log(new Circle().radius)Output:
1The radius field is declared right in the class and automatically gets the value 1 on every instance.
Key rules:
- ✓constructor and this are the base: new calls the constructor, this refers to the new object.
- ✓Public fields are declared right in the class body and initialized on every instance.
Common mistakes:
- !A class cannot be called without new — it throws a TypeError.
Private fields start with # and are only available inside the class itself — they cannot be accessed from outside. A getter (get) computes a value when accessed via a dot and is named like an ordinary property.
- Hide the internal state and allow it to be changed only by the class methods.
- Compute a value on the fly without an explicit method call.
class Counter {
#count = 0
increment() {
this.#count++
return this.#count
}
}
const c = new Counter()
console.log(c.increment())Output:
1#count is a private field: it can be changed only from the class methods. increment() increased it once → 1.
class User {
constructor(name) {
this.name = name
}
get upper() {
return this.name.toUpperCase()
}
}
console.log(new User("Anna").upper)Output:
ANNAThe upper getter is read like an ordinary property, but its value is computed when it is accessed.
Key rules:
- ✓A private field is marked with # and is not accessible outside the class.
- ✓A getter (get) is read via a dot and computes its value on the fly.
Common mistakes:
- !Accessing a # field outside the class is a syntax error SyntaxError.
A class inherits another via extends. The keyword super calls the parent constructor (super()) or a method (super.method()). Static methods belong to the class itself, not to an instance, and instanceof checks whether an object belongs to a class or its descendants.
- Reuse and extend the behavior of the parent class.
- Organize utilities at the class level and check object types.
class Animal {
constructor(name) {
this.name = name
}
speak() {
return "Sound"
}
}
class Dog extends Animal {
speak() {
return super.speak() + "!"
}
}
console.log(new Dog("Rex").speak())Output:
Sound!Dog inherits Animal; super.speak() calls the parent method, and the child adds the "!" suffix.
class MathHelp {
static double(n) {
return n * 2
}
}
console.log(MathHelp.double(4))Output:
8A static method is called via the class name (MathHelp.double), not via an instance.
Key rules:
- ✓extends — inheritance; super() — the parent constructor; super.method() — the parent method.
- ✓static — a method/field on the class itself; instanceof checks whether an object belongs to a class.
Common mistakes:
- !In a child constructor super() must be called before accessing this.