String interpolations and template literals

Hey all, I finished the introduction to Javascript part of the webdev course and am making notes during and after the lessons but I don’t quite understand the difference between string interpolations and template literals.

I am using this as a reference:

But the example given as a string interpolation is Tommy is ${age} years old.; and the example for a template literal: console.log(Hello, ${name}); look quite the same to me, except that the template literal example is being printed, and the string interpolation example isn’t. It also says that template literals are used in string interpolations, so can template literals be anything you want it to be and not only an existing variable/string? Because the second example of a template literal given (below) in the template literal is 6+8, but it isn’t a variable already created.
console.log(Billy is ${6+8} years old.) (second example)

Can someone please explain them to me in javascript-baby-language? I do not have a problem with understanding the difference between string interpolations and concatenations btw, only the interpolations and what template literals are.

String interpolation is the process of evaluating string literals containing one or more placeholders (expressions, variables, etc).

When you use template literals, you are using this process (String Interpolation).


Thankyou for your reply! Does this mean that a template literal is just a synonym for a placeholder in a string interpolation?

//template literal == an example of a string literal
console.log(`This is my string ${placeholder}`)

Essentially, yes.

It is important to interpret the term, interpolation correctly. It has nothing to do with the new syntax. It is about representing any value, how different it may be from a string object, as a string, within a string. That’s what we are seeking to achieve.

The new syntax just makes it easier.

Template literals are the death knell of string concatenation, and thank the heavens.

Thankyou! Your reply helped me out with better understanding :slight_smile:

1 Like