Class
In one line
A class is a blueprint for creating objects with predefined properties and methods, introduced in ES6 as syntactic sugar over prototypes.
In simple words
A class is a template for creating objects. It defines what properties and methods all instances of the class will have. ES6 class syntax makes object-oriented code in JavaScript look more like class-based languages, but under the hood it still uses prototypal inheritance.
A class has a constructor method that runs when you create a new instance with new. Methods defined in the class body are added to the prototype. You can extend classes with extends and override methods in subclasses.
Classes support getters, setters, static methods, and private fields (with # prefix in modern JavaScript). However, JavaScript classes are still fundamentally prototypes — the class keyword is syntactic convenience.
Example
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() { return `Hi, I'm ${this.name}`; }
}
const ada = new Person("Ada", 36);
console.log(ada.greet()); // "Hi, I'm Ada"A class with a constructor and method. new creates an instance.
Related terms
Related Resources
Related Lessons
- Objects
Learn about classes.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.