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 trialFABIOLA GONZALEZ
268 PointsConfusion about Template Literal Interpolation
the quiz states: Convert the set of concatenated strings assigned to the drink variable to a template literal. The final string should be "Blueberry Smoothie: $4.99".
const flavor = "Blueberry"; const type = "Smoothie"; const price = 4.99;
(answer) const drink = ${flavor} ${type}: $${price}
;
why was it not accepted to write it as:
const drink = $(flavor +type}:$${price}
;
*This seems like a much more simplified way to concatenate all the strings as well as a previous example listed as such. Can someone please explain why it wasn't accepted as true?
1 Answer
Jennifer Nordell
Treehouse TeacherHi there, FABIOLA GONZALEZ! The reason that wouldn't work comes down to spacing.
I'd like for you to try these two in your console.
const student = "Fabiola";
const greeting = "Hello there";
const test1 = `${greeting} ${student}`;
const test2 = `${greeting + student}`;
console.log(test1);
console.log(test2);
The first console.log()
will print out:
Hello there Fabiola
But the second one will print out:
Hello thereFabiola
Essentially, you are negating the template literal. That becomes a normal string concatenation when they are added inside the curly braces. You can even do arithmetic inside template literals.
Hope this helps!