0

I want to replace the console.log output with a windows alert displaying all persons data.

class person {
  constructor(name, job, color, age, licExpiryDate) {
    this.personsname = name;
    this.jobtitle = job;
    this.haircolor = color;
    this.personsage = age;
    this.licenseExpiryDate = licExpiryDate;
  }
}
let person1 = new person('David Smith', 'Marketing Manager', 'Brown', 25, '2020-06-01');
console.log(person1);

Tried replacing console.log with alert( person1 ) ; but did not work.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 2
    "did not work"; did anything happen? error in the console? I think `alert()` requires a string. – mykaf Feb 27 '23 at 17:43

2 Answers2

2

alert() converts its argument to a string, and objects by default convert to just [object Object].

To get a string that actually shows the contents of the object, use JSON.stringify().

class person {
  constructor(name, job, color, age, licExpiryDate) {
    this.personsname = name;
    this.jobtitle = job;
    this.haircolor = color;
    this.personsage = age;
    this.licenseExpiryDate = licExpiryDate;
  }
}
let person1 = new person('David Smith', 'Marketing Manager', 'Brown', 25, '2020-06-01');
alert(JSON.stringify(person1, null, 4));
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Try alert(JSON.stringify(person1))

HiDigiTech
  • 11
  • 3