How do I use special characters in a string like $ while using ${ }

I want the string to say “Today’s Special is Pizza for $5”
It seems simple but I can’t find anything in the documentation.

get todaysSpecial() {
if (this._meal && this._price){
return Today's Special is ${this._meal} for ${this._price};

I know I could just spell out “5 dollars”, but I’m wondering if there is a way to use special characters in strings.

https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-22-javascript-syntax-part-ii/modules/wdcp-22-learn-javascript-syntax-objects/projects/meal-maker

You can use \$ to get a $ into the string.

let cost = 5; console.log(`It cost \$${cost}`);

and similarly with \" and \' and \`

1 Like

That works great! Thank you!

You shouldn’t need to use the escape character \ for a dollar sign in a template literal or any other string literal. You would need it for including quotation marks inside of the same quotation marks.

let cost = 5; // no escape character needed console.log(`The cost is $${cost}`); console.log("The cost is $" + cost); console.log('The cost is $' + cost); console.log(`The storekeeper said, "The total cost is $${cost}"`); console.log('The storekeeper said, "The total cost is $' + cost + '"'); // escape character needed console.log("The storekeeper said, \"The total cost is $" + cost + "\"");
2 Likes

Thanks for all the examples!

1 Like