Introduction

suggest change

Template literals act like strings with special features. They are enclosed by backticks (`) and can span multiple lines.

Template Literals can contain embedded expressions too. These expressions are indicated by a $ sign and curly braces {}

// A single line Template Literal
const aLiteral = `single line string data`;

// Template Literal that spans across lines
const anotherLiteral = `string data that spans
         across multiple lines of code`;

// Template Literal with an embedded expression
const x = 2;
const y = 3;
const theTotal = `The total is ${x + y}`;     // Contains "The total is 5"

// Comparison of a string and a template literal
const aString = "single line string data"
console.log(aString === aLiteral)                         // Returns true

Tagged template literals and the raw property are covered on the following pages.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents