JavaScript String Type

Formatting

We can format strings any way we want with the methods and properties we have already covered, but still manual concatenation feels clunky.

Lucky for us, ECMAScript 2015 (all major browsers support the features in this standard) provides something called template literals.

Template literals are string literals that allow embedded expressions. These literals can be multi-line and support string interpolation. The embedded expressions can do pretty much anything as long as it's valid JavaScript and they return a value that can be coerced to a string value.

Template literals can be created by wrapping a template in backtick characters.

Code example: interpolation

const greet = (name) => {
return `Howdy, ${name}!`
}
console.log(greet("neighbor")) // "Howdy, neighbor!"

In the greet function we use string interpolation to replace the placeholder inside the template literal with the actual parameter.

Code example: coercion

const title = "Gardens of the Moon"
const year = 1999
const combined = `${title} - ${year}`
console.log(combined) // "Gardens of the Moon - 1999"

Here we can see that the integer number 1999 was coereced to a string value.

Read more on MDN

Template literals (Template strings)