Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them. They were called "template strings" in prior editions of the ES2015 specification.
`string text` `string text line 1 string text line 2` `string text ${expression} string text` tag `string text ${expression} string text`
Template literals are enclosed by the back-tick (` `) (grave accent) character instead of double or single quotes. Template literals can contain place holders. These are indicated by the Dollar sign and curly braces (${expression}
). The expressions in the place holders and the text between them get passed to a function. The default function just concatenates the parts into a single string. If there is an expression preceding the template literal (tag
here), the template string is called "tagged template literal". In that case, the tag expression (usually a function) gets called with the processed template literal, which you can then manipulate before outputting. To escape a back-tick in a template literal, put a backslash \ before the back-tick.
`\`` === '`' // --> true
Any new line characters inserted in the source are part of the template literal. Using normal strings, you would have to use the following syntax in order to get multi-line strings:
console.log('string text line 1\n' + 'string text line 2'); // "string text line 1 // string text line 2"
To get the same effect with multi-line strings, you can now write:
console.log(`string text line 1 string text line 2`); // "string text line 1 // string text line 2"
In order to embed expressions within normal strings, you would use the following syntax:
var a = 5; var b = 10; console.log('Fifteen is ' + (a + b) + ' and\nnot ' + (2 * a + b) + '.'); // "Fifteen is 15 and // not 20."
Now, with template literals, you are able to make use of the syntactic sugar making substitutions like this more readable:
var a = 5; var b = 10; console.log(`Fifteen is ${a + b} and\nnot ${2 * a + b}.`); // "Fifteen is 15 and // not 20."
A more advanced form of template literals are tagged template literals. Tags allow you to parse template literals with a function. The first argument of a tag function contains an array of string literals. The remaining arguments are related to the expressions. In the end, your function can return your manipulated string (or it can return something completely different as described in the next example). The name of the function used for the tag can be named whatever you want.
var person = 'Mike'; var age = 28; function myTag(strings, personExp, ageExp) { var str0 = strings[0]; // "that " var str1 = strings[1]; // " is a " // There is technically a string after // the final expression (in our example), // but it is empty (""), so disregard. // var str2 = strings[2]; var ageStr; if (ageExp > 99){ ageStr = 'centenarian'; } else { ageStr = 'youngster'; } return str0 + personExp + str1 + ageStr; } var output = myTag`that ${ person } is a ${ age }`; console.log(output); // that Mike is a youngster
Tag functions don't need to return a string, as shown in the following example.
function template(strings, ...keys) { return (function(...values) { var dict = values[values.length - 1] || {}; var result = [strings[0]]; keys.forEach(function(key, i) { var value = Number.isInteger(key) ? values[key] : dict[key]; result.push(value, strings[i + 1]); }); return result.join(''); }); } var t1Closure = template`${0}${1}${0}!`; t1Closure('Y', 'A'); // "YAY!" var t2Closure = template`${0} ${'foo'}!`; t2Closure('Hello', {foo: 'World'}); // "Hello World!"
The special raw
property, available on the first function argument of tagged template literals, allows you to access the raw strings as they were entered, without processing escape sequences.
function tag(strings, ...values) { console.log(strings.raw[0]); } tag`string text line 1 \n string text line 2`; // logs "string text line 1 \n string text line 2" , // including the two characters '\' and 'n'
In addition, the String.raw()
method exists to create raw strings just like the default template function and string concatenation would create.
var str = String.raw`Hi\n${2+3}!`; // "Hi\n5!" str.length; // 9 str.split('').join(','); // "H,i,\,n,5,!"
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Template Literals' in that specification. | Standard | Initial definition. Defined in several section of the specification: Template Literals, Tagged Templates |
ECMAScript 2017 Draft (ECMA-262) The definition of 'Template Literals' in that specification. | Draft | Defined in several section of the specification: Template Literals, Tagged Templates |
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 41 | (Yes) | 34 (34) | No support | 28 | 9 |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | No support | 41 | 34.0 (34) | No support | 28 | 9 |
String
String.raw()
© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals