Classes in JavaScript are templates for creating objects. They can contain properties and methods, providing a way to model real-world entities.
Classes are defined using the class
keyword:
class Car {
constructor(make, model) {
this.make = make;
this.model = model;
}
displayInfo() {
console.log(`Car: ${this.make} ${this.model}`);
}
}
let myCar = new Car("Toyota", "Corolla");
myCar.displayInfo(); // Outputs: Car: Toyota Corolla
Create a class for a "Person" with properties like "name" and "age" and a method to display those details.