Write a JavaScript program to create a class called “Person” with properties for name, age and country. Include a method to display the person’s details. Create two instances of the ‘Person’ class and display their details.
Plain code approach
In the above exercise we create a “Person” class with properties for name, age, and country. It includes a method to display the person’s details. Finally it creates two instances of the Person class and displays their details using the displayDetails() method.
Solution:
classPerson{constructor(name,age,country){this.name=name;this.age=age;this.country=country;}displayDetails(){console.log(`Name: ${this.name}`);console.log(`Age: ${this.age}`);console.log(`Country: ${this.country}`);}}// Create instances of the Person class
constperson1=newPerson('Francisca Rohan',25,'USA');constperson2=newPerson('Raimond Aruna',30,'Netherlands');// Display details of person1
console.log('Person-1 Details:');person1.displayDetails();// Display details of person2
console.log('\nPerson-2 Details:');person2.displayDetails();