Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

132
Views
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()

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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;
  },
};
about 4 years ago · Juan Pablo Isaza Report

0

You would either want to capture the return value of the function or call it so:

console.log(gix.age());

Think of age as a pointer to the function...

about 4 years ago · Juan Pablo Isaza Report

0

When you're console logging a function it wont execute it, It will just show the contents of the function, IF you need to see the value then you need to call the function and log the output

console.log(gix.age());

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!