Explanation on $ and ``?


#1

In the looping through objects lesson, we console log a combination of text and variables using `` instead of ‘’. We also use the $ symbol to access our variables while we’re in there. I can’t find a lesson on either of those symbols in the Javascript documentation or this course. Can someone link me to a place where I can learn more about them?

https://www.codecademy.com/courses/learn-javascript-objects/lessons/objects/exercises/for-in?action=resume_content_item&course_redirect=introduction-to-javascript


#2

What you refer to is a template literal which is a string intepolation tool.

Template literals are strings, but they are delimited with backticks, instead of ordinary quotes.

template = `This is still a string.`

In vanilla JS we would join strings using concatenation

stringA + stringB

With a template literal we can interpolate the variables in a template expression.

stringA = "This";
stringB = "string";
template = `${stringA} is still a ${stringB}.`

#3

That clears up a lot. Thank you.


#4

Are Template Literals covered in this course anywhere?


#5

I’m not sure if or where they are brought up. Not much to know beyond the example above. It should be noted that a template expression can contain another template literal. It is also perfectly valid to the use backticks on plain strings.

Template literals