Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript ES6 console.log object using template literal

I have simple object that I want to display in console

var obj = { name: 'John', age: 22 }

If I type:

console.log(obj)

Object { name: "John", age: 22 }

If I type:

console.log('my object is: ' + obj)

my object is: [object Object]

console.log('my object is: %o', obj)

my object is: Object { name: "John", age: 22 }

How can I achieve this using a template literal?

If I type:

console.log(`my object is: ${obj}`)

my object is: [object Object]

like image 868
robert Avatar asked Jun 16 '17 20:06

robert


1 Answers

You could serialize the object with JSON.stringify.

var obj = { name: 'John', age: 22 };
console.log(`my object is: ${JSON.stringify(obj)}`);
like image 52
Nina Scholz Avatar answered Oct 19 '22 11:10

Nina Scholz