Reputation: 1519
I have a function which prints an template literal:
function func() {
const obj = {
a: 1,
b: 2
};
console.log(`obj = ${obj}`);
}
It prints "obj = [object Object]".
If I want to log the content of the object(prints "obj = {a: 1, b: 2}"), how can I revise the code?
Upvotes: 3
Views: 1720
Reputation: 196236
JSON stringify it.
function func() {
const obj = {
a: 1,
b: 2
};
console.log(`obj = ${JSON.stringify(obj)}`);
}
func();
Upvotes: 4