简体   繁体   English

Visual Studio 代码无法在 javascript 编辑器中识别 $

[英]Visual studio code is not recognising $ in javascript editor

I am writing the following code in my VS code editor.我正在我的 VS 代码编辑器中编写以下代码。

let name="Sam";
const   val =  "I am  $ {name}";

console.log(val);

when I display it in console it should display" I am Sam" .But it is printing "I am $ {name}"当我在控制台中显示它时,它应该显示“我是山姆”。但它正在打印“我是 $ {name}”

I have added extention of "JS ES6" and "Live Server" I am a beginner and unable to understand what I am doing wrong.我添加了“JS ES6”和“Live Server”的扩展我是初学者,无法理解我做错了什么。

You need to define it as a template string, replace the " with ` and for the variable you have to remove the space between $ and {您需要将其定义为模板字符串,将 " 替换为 ` 并且对于变量,您必须删除 $ 和 { 之间的空格

 let name="Sam"; const myval = `I am ${name}`; console.log(myval);

For advanced description see here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals有关高级说明,请参见此处: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM