Back to results for “Objects”
JavaScript › Classes & Class Syntax

What are class fields, and how do they differ from constructor-assigned properties?

Intermediate

Answer

Class fields let you declare and initialize instance properties directly in the class body, outside the constructor, which runs before the constructor body executes. They reduce boilerplate especially with inheritance and default values.
class Counter {
  count = 0; // class field
  increment() { this.count++; }
}
Real-world example Declaring default component state fields cleanly at the top of a class, without cluttering the constructor.

Common follow-ups: Are class fields defined before or after super() runs in a subclass?