Advance JavaScript

JavaScript – Template Literals

Template literals are string literals which allows you to create multiline strings and string interpolation.

Template literals are new feature in ECMAScript was first introduced in ES6. It is also called template strings.

Before of ES6, template literals were called as template strings.

Template literals are enclosed by backtick(` `) symbol and variable which need to be concatenate should be ($(expression)) format.

Example: Concatenate a string to a variable

    var tutorialName = "Template Strings";
    var newTutorialName = `Advance JS ${tutorialName}`;

Advance JS Template Strings

Example: Multiline String

    console.log(`Using template literal 
multiline string`);  

Using template literal 
multiline string

A program using template literals to display first and last name together.

    let firstName   = "Share";
    let lastName    = "Query";

    function fullName(firstName, lastName)
        return `${firstName} ${lastName}`;

    let full_name =  `Hello ${fullName(firstName, lastName)}`;

Hello Share Query