如何在 JavaScript 中进行字符串插值? [英] How can I do string interpolation in JavaScript?
本文介绍了如何在 JavaScript 中进行字符串插值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑这个代码:
var age = 3;
console.log("I'm " + age + " years old!");
除了字符串连接之外,还有其他方法可以将变量的值插入到字符串中吗?
Are there any other ways to insert the value of a variable in to a string, apart from string concatenation?
推荐答案
从 ES6 开始,你可以使用 模板文字:
Since ES6, you can use template literals:
const age = 3
console.log(`I'm ${age} years old!`)
P.S. 注意反引号的使用:``
.
P.S. Note the use of backticks: ``
.
这篇关于如何在 JavaScript 中进行字符串插值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文