Our volunteers haven't translated this article into عربي yet. Join us and help get the job done!
You can also read the article in English (US).

The class declaration creates a new class with a given name using prototype-based inheritance.

You can also define a class using a class expression. But unlike the class expression, the class declaration doesn't allow an existing class to be declared again and will throw a type error if attempted. 

Syntax

class name [extends] {
  // class body
}

Description

The class body of a class declaration is executed in strict mode. The constructor property is optional.

Class declarations are not hoisted (unlike function declarations).

Examples

A simple class declaration

In the following example, we first define a class named Polygon, then extend it to create a class named Square. Note that super(), used in the constructor, can only be used in constructors and must be called before the this keyword can be used.

class Polygon {
  constructor(height, width) {
    this.name = 'Polygon';
    this.height = height;
    this.width = width;
  }
}

class Square extends Polygon {
  constructor(length) {
    super(length, length);
    this.name = 'Square';
  }
}

Attempting to declare a class twice

Re-declaring a class using the class declaration throws a type error.

class Foo {};
class Foo {}; // Uncaught TypeError: Identifier 'Foo' has already been declared

The same error is thrown when a class has been defined before using the class expression.

var Foo = class {};
class Foo {}; // Uncaught TypeError: Identifier 'Foo' has already been declared

Specifications

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Class definitions' in that specification.
Standard Initial definition.
ECMAScript 2016 (ECMA-262)
The definition of 'Class definitions' in that specification.
Standard  
ECMAScript 2017 (ECMA-262)
The definition of 'Class definitions' in that specification.
Standard  
ECMAScript Latest Draft (ECMA-262)
The definition of 'Class definitions' in that specification.
Draft  

Browser compatibility

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung InternetNode.js
Basic supportChrome Full support 42Edge Full support YesFirefox Full support 45IE No support NoOpera ? Safari Full support 10.1WebView Android Full support 42Chrome Android Full support 42Edge Mobile Full support YesFirefox Android Full support 45Opera Android ? Safari iOS Full support 10.1Samsung Internet Android Full support 4.0nodejs Full support 6.0.0
Array subclassingChrome Full support 43Edge ? Firefox ? IE No support NoOpera ? Safari ? WebView Android Full support 43Chrome Android Full support 43Edge Mobile ? Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android Full support 4.0nodejs ?
Allowed in sloppy modeChrome Full support 49Edge ? Firefox ? IE No support NoOpera ? Safari ? WebView Android Full support 49Chrome Android Full support 49Edge Mobile ? Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android Full support 5.0nodejs ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown

See also

Document Tags and Contributors

آخر مَن حدّثها: schalkneethling,