class expression

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

You can also define classes using the class declaration.

Try it


class {
  // class body
class name {
  // class body

Note: An expression statement cannot begin with the keyword class to avoid ambiguity with a class declaration. The class keyword only begins an expression when it appears in a context that cannot accept statements.


A class expression is very similar to, and has almost the same syntax as, a class declaration. As with class declarations, the body of a class expression is executed in strict mode. The main difference between a class expression and a class declaration is the class name, which can be omitted in class expressions to create anonymous classes. Class expressions allow you to redefine classes, while redeclaring a class using class declarations throws a SyntaxError. See also the chapter about classes for more information.


A simple class expression

This is just a simple anonymous class expression which you can refer to using the variable Foo.

const Foo = class {
  constructor() {}
  bar() {
    return "Hello World!";

const instance = new Foo();; // "Hello World!"; // "Foo"

Named class expressions

If you want to refer to the current class inside the class body, you can create a named class expression. The name is only visible within the scope of the class expression itself.

const Foo = class NamedFoo {
  constructor() {}
  whoIsThere() {
const bar = new Foo();
bar.whoIsThere(); // "NamedFoo"; // ReferenceError: NamedFoo is not defined; // "NamedFoo"


ECMAScript Language Specification
# sec-class-definitions

Browser compatibility

BCD tables only load in the browser

See also