如何将多个变量插入到一个字符串中?

How to insert multiple variables into a string?

我是 JavaScript 的新手,我想知道如何将多个变量插入到字符串中?

var adjective1 = 'amazing';
var adjective2 = 'fun';
var adjective3 = 'entertaining';



var madLib = "The Intro to JavaScript course is (adjective1). James and Julia are so (adjective2). I cannot wait to work through the rest of this (adjective3) content!";
console.log(madLib);

您可以使用Template literals (Template strings)

Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them. They were called "template strings" in prior editions of the ES2015 specification.

var adjective1 = 'amazing';
var adjective2 = 'fun';
var adjective3 = 'entertaining';

var madLib = `The Intro to JavaScript course is ${adjective1}. James and Julia are so ${adjective2}. I cannot wait to work through the rest of this ${adjective3} content!`;
console.log(madLib);