Converting expressions with template literals

2019-08-09 19:09发布

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! (=

3条回答
爷的心禁止访问
2楼-- · 2019-08-09 19:31

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

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

查看更多
成全新的幸福
3楼-- · 2019-08-09 19:36

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

查看更多
冷血范
4楼-- · 2019-08-09 19:43

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

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

查看更多
登录 后发表回答