DeveloperBreeze

JavaScript Class with Constructor and Method

javascript
// Class definition for Person
class Person {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }

    // Method to generate a greeting
    greet() {
        return `Hello, my name is ${this.name} and I am ${this.age} years old.`;
    }
}

// Usage of the Person class
const person = new Person('John', 30);
const greeting = person.greet();

Continue Reading

Handpicked posts just for you — based on your current read.

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!