class
Declares a class with a constructor, methods, and optional inheritance.
Syntax
class Name extends Parent {
constructor(...) { ... }
method() { ... }
}
Returns
undefined — A class declaration is a statement and does not produce a value.
Examples
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
toString() {
return `(${this.x}, ${this.y})`;
}
}
console.log(new Point(1, 2).toString());
Output
(1, 2)
class Animal {
speak() { return "..."; }
}
class Dog extends Animal {
speak() { return "Woof"; }
}
console.log(new Dog().speak());
Output
Woof
class Counter {
#count = 0;
increment() { return ++this.#count; }
}
const c = new Counter();
console.log(c.increment());
Output
1
Notes
- Class declarations are not hoisted in a usable way; they are in the
temporal dead zone until evaluated.
- Class bodies always run in strict mode.
- Private fields use a `#` prefix and are only accessible inside the
class body.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 49 |
| firefox | 45 |
| safari | 9 |
| edge | 13 |
| node | 6.0 |