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

366
Views
Javascript How to print an object using string interpolation

is there any way to print the object using string interpolation method?

const star = {
    id: 1,
    name: 'Arcturus',
    visualMag: -0.05,
};

This method doesnt work

console.log(`${star}`); // [object Object]

This works

console.log(`${star.name}`); // 'Arcturus'

and simply using console.log(star) works

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

0

Hey do this simple trick:

console.log({star});

Output:

[Log] {star: {id: 1, name: "Arcturus", visualMag: -0.05}}
about 4 years ago · Juan Pablo Isaza Report

0

You can use JSON.stringify

console.log(`${JSON.stringify(test)}`)
about 4 years ago · Juan Pablo Isaza Report

0

When you use an object within template literal, it coerces the object to a string. It looks for a toString method on the object. If it is not found, it will use Object.prototype.toString method which returns "[object Object]".

So, add a toString property to the object.

const star = {
  id: 1,
  name: 'Arcturus',
  visualMag: -0.05,
};

Object.defineProperty(star, "toString", {
  value: function() {
    return JSON.stringify(this)
  }
})

console.log(`${star}`);

Note: You could directly add star.toString = function() { ... }. But, that will add an enumerable property to the object and will be displayed when you log the object directly.

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!