This approach is straightforward but it is not very flexible, especially when you want to use multiple variables.
String Interpolation using Template Literals
The more modern way to do string interpolation is to use template literals.
Template literals are a new way to write string literals that are more flexible because you can use variables and expressions inside of the string.
JAVASCRIPT
const name = "John";
const greeting = `Hello ${name}`;
console.log(greeting);
BASH
Hello John
You can define a template literal by using the backtick (`) character, then inserting variables by using the dollar sign ($) character and enclosing the variable in curly braces.
JAVASCRIPT
const name = "John";
const greeting = `Hello ${name}`;
console.log(greeting);
BASH
Hello John
The cool thing about string interpolation with template literals is that you can use any JavaScript expression inside of the string.
JAVASCRIPT
const name = "John";
const greeting = `Hello ${name.length}`;
console.log(greeting);
BASH
Hello 4
This also means you can call methods from inside of the string as well: