From the course: Learn JavaScript: Write Modern Code with JavaScript ESNext

Unlock this course with a free trial

Join today to access over 25,000 courses taught by industry experts.

Use interpolation to insert data into strings

Use interpolation to insert data into strings

- The next ES6 syntax we're going to look at is something called template strings or template literals. Now, earlier in the course when we were first talking about the string data type in JavaScript, we mentioned that there were three main ways to define strings, using single quotes, using double quotes, and using backticks. And as a matter of fact, in ES6, backtick strings have a special use case that we're going to look at now, and that's when we need to insert data into strings. Prior to ES6, whenever we wanted to insert the values of variables into strings, we had to either use the plus sign or the built-in string .concat method to do so. And both of these serve their purposes well-enough, but in practice, they can be a little cumbersome to use. That, in addition to the fact that, as we saw earlier in the course, using the plus sign can have some unintended consequences. For example, if we have a string that says 2 + 2 is, and then add 2 + 2 to the end of it, we'll end up with 2 +…

Contents