Javascript - Template Literals and Delimiters

Introduction

ES6 introduces Template Literals, which support strings with additional functionalities like:

  • String interpolation
  • Embedded expressions
  • Multiline strings
  • String formatting

Template Literals use backticks (``).

A template literal can be written as follows:

let user = `Tom`; 

Template literals support string substitutions that substitute any valid JavaScript expression inside a string.

Template Literals can contain placeholders for string substitution using the ${ } syntax.

let user = `Tom`; 
console.log(`Hi ${user}!`); // Hi Tom! 

Here, the template literal is delimited by backticks (`) and the interpolated expressions inside the literal are delimited by ${ and }.

Template Literals can also support inline math, for example:

let a = 10; 
let b = 20; 

console.log(`Sum of ${a} and ${b} is ${a+b}`); 

Template literals can have multiline strings without the use of \n:

Demo

console.log(`line-one 
line-two`); /*  w ww  . j av  a 2 s.  c  o m*/

// line-one 
// line-two

Result