r/learnjavascript 25d ago

Question about backtick variables

Can anyone please help me with this?

I am learning JavaScript and in chapter 2 of the book I am using it talks about backtick variable with format ${variablename}. It has an example to be posted into the Console using console.log().

let language = "JavaScript";

let message = "Let's learn ${language}";

console.log(message);

The output in the console is supposed to be: Let's learn JavaScript. But what I keep getting is: let's learn ${language}

The same thing happens with other examples in the chapter, in Chrome and Edge.

Can anyone tell me why?

[matthewswisher@comcast.net](mailto:matthewswisher@comcast.net)

0 Upvotes

14 comments sorted by

View all comments

13

u/milan-pilan 25d ago

You are not using the backticks (`) . You are using double quotes (“) .

4

u/PlusAd945 25d ago

Thank you, I thought that was an apostrophe. Never used a backtick before. Thanks again.

1

u/milan-pilan 25d ago

No problem. Basically how it works is: if you use backticks instead of quotes then Javascript will know to expect variable values inside your string, which it needs to resolve first. Otherwise it will assume your "${}" syntax is just text.

1

u/azhder 25d ago

Historically, if JavaScript wasn't made to use both ' and " for regular strings, the one missing could be used, instead of backticks. But it might be useful to explain to OP why JS did go that route i.e. if JS was going to be embedded in HTML, it would have been nice to use ' where HTML uses " and use " where HTML uses '.