Template Strings

Template Strings use back-ticks `...` rather than the single or double quotes we're used to with regular strings.

var greeting = `Hello World!`;

String interpolation

Expressions can be included within strings using $(...) syntax, as below:

var name = 'World!'; console.log(`Hello ${name}`); // Hello World!

The above code (ES6 standard) is equal to the below code

var name = 'World!'; console.log("Hello " + name);

Other use case examples

var a = 10, b = 20; console.log(`Addition value for ${a} and ${b} is ${a+b}`); // Addition value for 10 and 20 is 30 var user = {name: 'gns'}; console.log(`Current User: ${user.name.toUpperCase()}.`); // Current User: GNS.

Multiline Strings

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 string text line 2`); // "string text line 1 // string text line 2"

The above code (ES6 standard) is equal to the below code

console.log("string text line 1\n"+ "string text line 2"); // "string text line 1 // string text line 2"