Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKayc M
12,593 PointsCan't log map using template literals, why?
let joe = { name: 'joe', age: 20},
ann = {name: 'ann', age: 20 };
let classroom = new Map()
classroom.set('joe', joe)
classroom.set('ann', ann)
console.log('print map: ', classroom)
output:
// print map: Map(2) {
// 'joe' => { name: 'joe', age: 20 },
// 'ann' => { name: 'ann', age: 20 }
// }
console.log(`print map: ${classroom}`)
output:
// print map: [object Map]
2 Answers
Steven Parker
231,261 PointsModern browsers may evaluate the arguments to the "log" method and invoke a special conversion designed to display that type of object, but the template string relies on the object's own string conversion method.
So:
console.log(`print map: ${classroom}`); // is equivalent to...
console.log('print map: ' + classroom.toString());
Kayc M
12,593 PointsBut why does concatenating without "toString" have the same result
console.log('print map: ' + classroom);
output:
// print map: [object Map]
Steven Parker
231,261 PointsConcatenating a non-string onto a string also invokes the built-in (toString) conversion. What the browser does for you when it is a separate argument is a new feature above and beyond the language itself, and not long ago in previous versions would have looked the same as the others.