Define a class with properties and methods in javascript

In JavaScript, you can define a class using the class keyword. Here is an example of how to define a class with properties and methods:

 
class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
  
  greet() {
    console.log(`Hello, my name is ${this.name} and I'm ${this.age} years old.`);
  }
  
  celebrateBirthday() {
    this.age++;
    console.log(`It's my ${this.age}th birthday!`);
  }
}
 
 

In this example, we define a Person class with two properties (name and age) and two methods (greet and celebrateBirthday). The constructor method is used to initialize the properties of the class.

To create an instance of the class, you can use the new keyword:

const john = new Person('John', 30);

This creates a new Person object with the name "John" and age 30. You can then call methods on the object:

 
john.greet(); // logs "Hello, my name is John and I'm 30 years old."
 
john.celebrateBirthday(); // logs "It's my 31st birthday!"
 
 

Note that the `this` keyword is used to refer to the current object (i.e., the instance of the class) within the methods.