如何在JavaScript中进行字符串插值? [英] How can I do string interpolation in JavaScript?

查看:89
本文介绍了如何在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:

let age = 3;
console.log(`I'm ${age} years old!`);

PS 请注意使用反引号:``

P.S. Note the use of backticks: ``.

这篇关于如何在JavaScript中进行字符串插值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆