classes

JavaScript Classes

JavaScript Classes

Classes in JavaScript are templates for creating objects. They can contain properties and methods, providing a way to model real-world entities.

Creating a Class

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
      
    

Activity

Try It Yourself!

Create a class for a "Person" with properties like "name" and "age" and a method to display those details.

Quiz

Quick Quiz

  1. What is a class in JavaScript?
  2. <