Skip to content

class

In JavaScript, the class keyword defines a class, a constructor function with a body that declares its methods, fields, and accessors in one place. It works as a declaration that binds a name and as a class expression that produces a value, and both build on the prototype model rather than replacing it.

A class declaration is block-scoped like let, and reading the name before the declaration runs throws a ReferenceError because of the temporal dead zone. The class body always runs in strict mode. typeof reports a class as "function", but calling it without new throws a TypeError. A class inherits from another with extends, calls the parent with super, and marks members that belong to the class itself with static.

Classes were added in ES2015. Public and private fields, #private methods, and static blocks were added in ES2022.

JavaScript class Keyword Syntax

A class can be defined with a declaration or an expression:

Language: JavaScript Syntax
class <Name> [extends <Parent>] {
  [<members>]
}

const <binding> = class [<Name>] [extends <Parent>] {
  [<members>]
};

The first form is a declaration and needs a name, except as a default export. The second is an expression, where the name is optional and visible only inside the class body.

JavaScript class Keyword Examples

The Counter class keeps a private count and shares one increment() method across instances:

Language: JavaScript
> class Counter {
…   #count = 0;
…   increment() {
…     this.#count += 1;
…     return this.#count;
…   }
… }
undefined

> const clicks = new Counter();
undefined
> clicks.increment()
1
> clicks.increment()
2

> typeof Counter
'function'

The private field #count starts at 0 for each instance, and increment() lives on Counter.prototype, so every instance shares it.

Calling a class like a regular function throws an error:

Language: JavaScript
> class Point {}
undefined
> Point()
Uncaught TypeError: Class constructor Point cannot be invoked without 'new'

JavaScript class Keyword Use Cases

  • Grouping an object’s state and the methods that work on it
  • Creating many instances that share the same methods
  • Hiding internal state behind #private fields
  • Building a subclass of a built-in such as Error or Map

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 4, 2026