简体   繁体   中英

Visual studio code is not recognising $ in javascript editor

I am writing the following code in my VS code editor.

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}"

I have added extention of "JS ES6" and "Live Server" I am a beginner and unable to understand what I am doing wrong.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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