JavaScript JavaScript Basics Working with Strings Write a Template Literal

Olga Pokrovskaya
Olga Pokrovskaya
1,880 Points

const flavor = "Blueberry"; const type = "Smoothie"; const price = 4.99; const drink = flavor + ' ' + type + ': ' + '$'

interpolation (${ }) - what is this bummer about? Here is what I write

nst drink = `${Blueberry} ${Smoothie}: $${4.99}

const flavor = "Blueberry";
const type = "Smoothie";
const price = 4.99;

const drink = flavor + ' ' + type + ': ' + '$' + price;
const drink = `${Blueberry} ${Smoothie}: $${4.99}`

2 Answers

Nikos Papapetrou
Nikos Papapetrou
5,577 Points

The challenge is to use the template literal: the backticks(in the left-top corner of your keyboard) the dollar sign and inside the brackets the variable you have declare.

`${ variable_name }`

Nicolette Grannum
Nicolette Grannum
Full Stack JavaScript Techdegree Student 10,670 Points

A template literal reads more naturally like a sentence. Instead of manually adding "(space)" each time you want a space in a string, you can simply type a space. The "${}" is just so the program knows that it is a variable and not a string.

For this challenge, like Nikos said, you will want to include the variable name inside the curly braces, not the value. This way, you do not need to manually update the string every time the flavor, type, or cost changes. You used the variable names in the first 'drink' constant, and you would use them the same way below.

Also, as a general note, if you have 2 const with the same name, it will give you an error even if the template literal is correct. Try using 'let' for the first and just 'drink=' for the second if you want to change the value. Or using a name other than 'drink' for your second line.