VS代码中无法识别字符串插值

问题描述 投票:-2回答:1

字符串内插被识别为字符串。

  let highScore = 1430;
let userScore = 1600;

if (userScore >= highScore) {
  console.log("Congrats, you have the new high score of ${userScore}");
}

console and vscode screenshot

javascript visual-studio-code
1个回答
0
投票

JavaScript仅支持使用反引号的模板文字中的字符串替换

let userScore = 1430;
let string1 = "Congrats, you have the new high score of ${userScore}";
let string2 = `Congrats, you have the new high score of ${userScore}`;

document.querySelector('#result1').textContent = string1;
document.querySelector('#result2').textContent = string2;
<pre id="result1"></pre>
<pre id="result2"></pre>

注意console.log itself does some substitutions。浏览器显然有所不同。

© www.soinside.com 2019 - 2024. All rights reserved.