Widely availableSupported across all major browsers. Safe to use in production.

Overview

Classes are an object-oriented syntax for JavaScript prototypes.

Browser support

Feature Desktop Mobile
Chrome
Edge
Firefox
Safari
Chrome Android
Safari iOS
49
13
45
10.1
49
10.3
Other

Classes are a template for creating objects. They encapsulate data with code to work on that data. Classes in JS are built on prototypes but also have some syntax and semantics that are unique to classes.

49
13
45
9
49
9

The constructor method is a special method of a class for creating and initializing an object instance of that class.

49
13
45
9
49
9

The extends keyword is used in class declarations or class expressions to create a class that is a child of another class.

49
13
45
9
49
9

Private elements are counterparts of the regular class elements which are public, including class fields, class methods, etc. Private elements get created by using a hash # prefix and cannot be legally referenced outside of the class. The privacy encapsulation of these class elements is enforced by JavaScript itself. The only way to access a private element…

74
79
90
14.1
74
14.5

Private elements are counterparts of the regular class elements which are public, including class fields, class methods, etc. Private elements get created by using a hash # prefix and cannot be legally referenced outside of the class. The privacy encapsulation of these class elements is enforced by JavaScript itself. The only way to access a private element…

91
91
90
15
91
15

Private elements are counterparts of the regular class elements which are public, including class fields, class methods, etc. Private elements get created by using a hash # prefix and cannot be legally referenced outside of the class. The privacy encapsulation of these class elements is enforced by JavaScript itself. The only way to access a private element…

84
84
90
15
84
15

Public fields are writable, enumerable, and configurable properties defined on each class instance or class constructor.

72
79
69
16
72
16

The static keyword defines a static method or field for a class, or a static initialization block (see the link for more information about this usage). Static properties cannot be directly accessed on instances of the class. Instead, they're accessed on the class itself.

49
13
45
9
49
9

Public fields are writable, enumerable, and configurable properties defined on each class instance or class constructor.

72
79
75
14.1
72
14.5

Static initialization blocks are declared within a Statements/class. It contains statements to be evaluated during class initialization. This permits more flexible initialization logic than Classes/static properties, such as using try...catch or setting multiple fields from a single value. Initialization is performed in the context of the current class…

94
94
93
16.4
94
16.4

The class keyword can be used to define a class inside an expression.

42
13
45
7
42
7

The new.target meta-property lets you detect whether a function or constructor was called using the new operator. In constructors and functions invoked using the new operator, new.target returns a reference to the constructor or function that new was called upon. In normal function calls, new.target is undefined.

46
13
41
11
46
11

The super keyword is used to access properties on an object literal or class's [[Prototype]], or invoke a superclass's constructor.

42
13
45
7
42
7
1+Supported (version) Not supported Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)
Notes 2 item(s)
Removed
  • This feature was removed in a later browser version (49)
Implementation note
  • Strict mode is required.
Notes 2 item(s)
Removed
  • This feature was removed in a later browser version (49)
Implementation note
  • Strict mode is required.
Notes 2 item(s)
Removed
  • This feature was removed in a later browser version (49)
Implementation note
  • Strict mode is required.
Notes 2 item(s)
Removed
  • This feature was removed in a later browser version (49)
Implementation note
  • Strict mode is required.
Notes 2 item(s)
Removed
  • This feature was removed in a later browser version (49)
Implementation note
  • Strict mode is required.
Notes 2 item(s)
Removed
  • This feature was removed in a later browser version (49)
Implementation note
  • Strict mode is required.
Notes 2 item(s)
Removed
  • This feature was removed in a later browser version (49)
Implementation note
  • Strict mode is required.
Notes 2 item(s)
Removed
  • This feature was removed in a later browser version (49)
Implementation note
  • Strict mode is required.
Notes 5 item(s)
Limitation
  • This browser only partially implements this feature
Removed
  • This feature was removed in a later browser version (16)
  • This feature was removed in a later browser version (14.1)
Implementation note
  • Parentheses in field initializers can lead to `ReferenceError`s. See bug 236843.
  • Doesn't support public static fields. See bug 194095.
Notes 5 item(s)
Limitation
  • This browser only partially implements this feature
Removed
  • This feature was removed in a later browser version (16)
  • This feature was removed in a later browser version (14.5)
Implementation note
  • Parentheses in field initializers can lead to `ReferenceError`s. See bug 236843.
  • Doesn't support public static fields. See bug 194095.
Notes 2 item(s)
Removed
  • This feature was removed in a later browser version (49)
Implementation note
  • Strict mode is required.
Notes 2 item(s)
Removed
  • This feature was removed in a later browser version (49)
Implementation note
  • Strict mode is required.

Syntax

JAVASCRIPT
class Animal {
  constructor(name) {
    this.name = name;
  }
  speak() {
    return `${this.name} barked`;
  }
}

class Dog extends Animal {
  speak() {
    return `${this.name}: Woof!`;
  }
}

new Dog('Pochi').speak(); // 'Pochi: Woof!'

Live demo

autocounter-class

Automaticallykauntapsimul-tion.. with Class usagestate manage, setTimeout.

PreviewFullscreen

asyncde-tareadclass

Inheritance use, class. feature extended async in de-ta readsimul-tion..

PreviewFullscreen

Calculation with staticmethod.

instansization to usage in staticmethod Use, delayafter to calculationresult display..

PreviewFullscreen

Use cases

  • Using Classes

    Classes are an object-oriented syntax for JavaScript prototypes.

Cautions

  • No specific concerns. Stable across all major browsers.

Accessibility

  • When updating the DOM dynamically, announce important changes to assistive technology with aria-live regions.

Powered by web-features