0

I'm taking Andrew Mead - Udemy course in Javascript. But I do have a question about the template string that was given on video.

return 'A ${percent}% tip on $${total} would be $${tip}'

This doesn't work when I'm typing it in vscode. However, for it to work in my vscode, I must type it in as

return "A " + percent  + "% tip on $" + total + " would be $" + tip;

I have Babel Javascript and Prettier installed but wondering what exactly the cause of this code is not working correctly? Whenever I run the node arguments.js, it prints out as "A ${percent}% tip on $${total} would be $${tip}"

If I do the node arguments.js for the second code, it does print out, "A 20% tip on $60 would be $12"

I want to learn how to get the strings to work in various ways possible. Thank you in advance.

ruohola
  • 21,987
  • 6
  • 62
  • 97
Terry Hunt
  • 41
  • 8
  • You need to use back tick ` which is located on the top left of the key board. This is different from quotes `'` – brk Jul 25 '21 at 10:46

1 Answers1

5

This:

return 'A ${percent}% tip on $${total} would be $${tip}'

was likely meant to be:

return `A ${percent}% tip on $${total} would be $${tip}`

Note the use of backticks ` instead of quotes '. They are needed to create a template literal.

ruohola
  • 21,987
  • 6
  • 62
  • 97