<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div>
</div>
</body>
<script>
let expression = '你好';
let str = `string text line 1
string text line 2`;
let a = 3;
let b = 5;
let inter = `string text ${expression} string text`;
document.write(str);
document.write(inter);
console.log("你好!");
console.log(`string text line 1
string text line 2`);
console.log(`模板字符串插入表达式:a + b = ${a+b},a * b = ${a*b}`+'\n普通字符串插入表达式: a + b = ' + (a + b) + ',a * b =' + (a * b));
</script>
</html>
从代码中我们可以看到,模板字符串有着普通字符串的优点,比如在普通字符串中换行需要使用"\n",而模板字符串是不需要的,直接换行打印出来就行,而且模板字符串的插值方式和普通字符串也不相同,普通字符串插值相对复杂,需要用到字符串连接符“+”,而模板字符串只需要在模板字符串中使用${value}插入你想要的方法和数值即可。 代码是最容易使人信服的一种方式,先运行一遍代码,带着问题去思考,相信你会有比我还要精深的理解。