0

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.

2
  • 1
    Use backticks `, not quotes ". Also it's ${name} not $ {name}. Commented Oct 21, 2021 at 14:59
  • Thanks a lot Jeremy. Commented Oct 21, 2021 at 15:17

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

I took the liberty to make a snippet so we can see your code in action.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.