Available in Chrome 41+
ES6 Template Literals are strings that can include embedded expressions. This is sometimes referred to as string interpolation.
Template Literals use back-ticks rather than the single or double quotes we're used to with regular strings.
The template literal can contain placeholders using the ${ }
syntax. The value populated in
placeholders including the text between them gets passed to a function. This is determined on the expression
before the template string. If no expression exists before the template literal, the default template is used.
This sample demonstrates creating and working with expression placeholders, expression interpolation, multi-line strings and functions inside expressions. We also look at tagged Template Literals.
Using a basic expression placeholder: Yo! My name is Addy Osmani! Using expressions work just as well with object literals: Thanks for getting this into V8, Caitlin Potter. Expression interpolation. One use is readable inline math: The number of JS frameworks is 150 and not 200. Multi-line strings without requiring a new line feed: string text line 1 string text line 2 string text line 3 Functions inside expressions: foo result bar Modifying output using an expression preceding the template literal (Tagged Template Literal): DID YOU KNOW 1 + 4 IS 5?