Javascript Object method value not showing with console.log of the object

I'm a bit confused with objects in JavaScript...

I wrote an object:

const gix = {
  firstName: "John",
  lastName: "Johnson",
  yearOfBirth: 2000,
  profession: "IT",
  friends: ["Mark", "Luke", "John"],
  driversLicence: true,
  age: function () {
    this.calcAge = 2022 - this.yearOfBirth;
    return this.calcAge;
  },
};

gix.age();
console.log(gix);

Why is the console log of the whole object not showing the calculated value but is showing age: f()


Solution 1:

Considering your use-case, you could replace the method with a getter, which gets evaluted each time the object is referenced:

const gix = {
  firstName: "John",
  lastName: "Johnson",
  yearOfBirth: 2000,
  profession: "IT",
  friends: ["Mark", "Luke", "John"],
  driversLicence: true,
  get age() {
    return 2022 - this.yearOfBirth;
  },
};