Advertisement
Intermediate JavaScript, Classes & Class Syntax

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

Short 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.

Save JavaScript mock interview
Advertisement
AModel 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.
Likely follow-ups
  • Are class fields defined before or after super() runs in a subclass?
Related topics