-->

Converting expressions with template literals

2019-08-09 19:00发布

问题:

When I use the console.log() method with a template literal and I interpolate the expression 5 + 5 within ${}, the result is not given in the console.

This is my code:

 <script>
      console.log('five plus five is ${5 + 5}');
 </script>

When I check the console, it just repeats, "five plus five is ${5 + 5}" without the quotes, of course.

What am I doing wrong?

Thanks! (=

回答1:

Wrap it inside back tick (`) character not inside single(') or double quotes ", See template literals

console.log(`five plus five is ${5 + 5}`);



回答2:

You are using the wrong "quotes" ... use the back-tick `

console.log(`five plus five is ${5 + 5}`)



回答3:

You can only use template literals with backticks.

Template literals are enclosed by the back-tick (grave accent) character instead of double or single quotes.

console.log(`five plus five is ${5 + 5}`);

Learn more about Template Literals at MDN