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 trial

JavaScript

Confusion 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
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi 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! :sparkles: