Search code examples
javascriptecmascript-6template-literals

Escape dollar sign in JavaScript template literals (template strings)


I am using the new template literals (template strings) syntax of JavaScript ES6 Docs Here and I am not quite sure how to escape the dollar sign that is used to break the string to add a parameter.

Here is what I am trying to do:

var response = `I consent to my credit card being charged in the amount of
                 $ ${ total } for the purchase of ${ item.title } and any
                 applicable sales tax.`

that works fine... but I would really prefer to not have that space $ ${title}

that leaves the end result looking like :

... in the amount of $ 25.99 for the purchase...

I would really rather prefer

... in the amount of $25.99 for the purchase ...

I guess that is ok, or obviously I could use the old way that still works, but it would be nice to know how to fix this. I linked to the Mozilla docs , and I can't find anything in there about it, hopefully someone has an idea how to fix this


Solution

  • var response = `I consent to my credit card being charged in the amount of
                     $${ total } for the purchase of ${ item.title } and any
                     applicable sales tax.`