JavaScript class Statement
The class declaration defines a blueprint for creating objects, with a constructor, methods, getters, static members and private fields, and can extend another class.
Description
A class declaration creates a class: a template for objects that share the same structure and behavior. The constructor sets up each new instance, methods are shared by all instances through the prototype, and get/set define computed properties. static members belong to the class itself, and names starting with # are truly private, accessible only inside the class.
extends creates a subclass, and super calls the parent's constructor or methods. Class bodies always run in strict mode, classes must be called with new, and unlike function declarations they aren't usable before the line that declares them.
Classes are mostly a cleaner syntax for JavaScript's existing prototype-based inheritance: methods live on ClassName.prototype, and typeof a class is "function". Class fields, written directly in the class body, give each instance its own property and run before the constructor body. A static { } block runs once when the class is defined, which is useful for more complex static set-up. Classes can also be written as expressions, const Model = class { ... }.
Syntax
class Name extends Parent {
#privateField = value;
static staticProp = value;
constructor(params) {
super(params);
}
method() {}
get prop() {}
}Example
Browser Support
The following information will show you the current browser support for the class statement. Hover over a browser icon to see the version that first introduced support for this statement.
Desktop





Tablets & Mobile





