Template literals are the standard way to perform string interpolation in modern JavaScript. They use backticks (`) instead of single or double quotes and allow you to embed expressions directly within strings using ${expression} syntax.
Key Features of Template Literals
Variable Interpolation: Embed variables directly.
const name = "Alice"; const message = `Hello, ${name}!`; // Output: "Hello, Alice!"Expression Support: Use any valid JavaScript expression inside
${}.const a = 5; const b = 10; const result = `The sum is ${a + b}.`; // Output: "The sum is 15."Multi-line Strings: Preserve line breaks and whitespace without using
\n.const html = ` <div> <h1>Hello</h1> <p>World</p> </div> `;Function Calls: Call functions directly within the template.
function getGreeting() { return "Welcome"; } const greeting = `${getGreeting()}!`;
Browser Support
Template literals are supported in all modern browsers (ES6+), including Chrome 41+, Firefox 34+, Safari 9+, and Edge 13+.
✅ Best Practice: Always use template literals (
) for string interpolation instead of concatenation (+`) for better readability and maintainability.
Since ES6, you can use template literals:
const age = 3
console.log(`I'm ${age} years old!`)
P.S. Note the use of backticks: ``.
Since ES6, you can use template literals:
const age = 3
console.log(`I'm ${age} years old!`)
P.S. Note the use of backticks: ``.
tl;dr
Use ECMAScript 2015's Template String Literals, if applicable.
Explanation
There is no direct way to do it, as per ECMAScript 5 specifications, but ECMAScript 6 has template strings, which were also known as quasi-literals during the drafting of the spec. Use them like this:
> var n = 42;
undefined
> `foo${n}bar`
'foo42bar'
You can use any valid JavaScript expression inside the {}. For example:
> `foo${{name: 'Google'}.name}bar`
'fooGooglebar'
> `foo${1 + 3}bar`
'foo4bar'
The other important thing is, you don't have to worry about multi-line strings anymore. You can write them simply as
> `foo
... bar`
'foo\n bar'
Note: I used io.js v2.4.0 to evaluate all the template strings shown above. You can also use the latest Chrome to test the above shown examples.
Note: ES6 Specifications are now finalized, but have yet to be implemented by all major browsers.
According to the Mozilla Developer Network pages, this will be implemented for basic support starting in the following versions: Firefox 34, Chrome 41, Internet Explorer 12. If you're an Opera, Safari, or Internet Explorer user and are curious about this now, this test bed can be used to play around until everyone gets support for this.
TIL JavaScript provides string interpolation with string template literals
I made a new string interpolation library (typed-string-interpolation)
Problem with Variable Passing in String Interpolation Function Arguments
Nesting string interpolations in the arguments of an interpolation is not supported. Use regular concatenation instead, e.g.:
"result="++matches(sample, expression["toMatch"])More on reddit.com
Some comments about the new String Templates feature
Videos
I started learning JavaScript in 2012 before this was full supported, never realized the backtick was used in JavaScript. It works great
const number = 2
const message = `The number is ${number}`
console.log(message); // => 'The number is 2'