JavaScript Getting Started With ES2015 Using Template Literals String Interpolation

SeHyun Choi
SeHyun Choi
3,441 Points

Does interpolation only work with template literal?

When I do

let name = "sehyun";
document.write(`Hi ${name}`);

It works but

When I do

let name = "sehyun";

document.write("hello " + ${name});

1 Answer

Steven Parker
Steven Parker
177,523 Points

That's right, interpolation only occurs inside a template string, and the token syntax is not valid outside of one. In your other example, you can simply concatenate the variable directly:

document.write("hello " + name);