Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

React ES6 Template Strings


Template Strings

Template strings allow you to write strings that span multiple lines and include embedded expressions:

Example

Before:

const name = "John";
const age = 30;
const message = "Hello, " + name + "!\n" + 
"You are " + age + " years old.";

Try it Yourself »

Example

With Template Strings:

const name = "John";
const age = 30;
const message = `Hello, ${name}!
You are ${age} years old.`;

Try it Yourself »

Template strings use backticks (`) instead of quotes and can include:

  • Multiple lines without \n
  • Expressions inside ${}
  • Quotes without escaping

Example

Multi-line Strings:

const html = `
  <div>
    <h1>Title</h1>
    <p>Paragraph</p>
  </div>
`;

Try it Yourself »

Note: The indentation in multi-line strings becomes part of the string.

Example

The indentation becomes part of the string:

const x = `
  John:
    Hello, how are you?
  Jane:
    I'm fine, thanks!
`;

Try it Yourself »


Expression Interpolation

You can include any valid JavaScript expression inside ${} in a template string:

Example

Insert variables inside template strings:

let firstName = "John";
let lastName = "Doe";

let text = `Welcome ${firstName}, ${lastName}!`;

Try it Yourself »

Example

Insert expressions inside template strings:

let price = 10;
let quantity = 5;

let total = `Total: ${price * quantity}`;

Try it Yourself »

Example

Using the map function inside template strings:

const items = ["apple", "banana", "orange"];
const list = `You have ${items.length} items:
${items.map(item => `- ${item}`).join('\n')}`;

Try it Yourself »

Example

Using ternery operator inside template strings:

const isAdmin = true;
const message = `Status: ${isAdmin ? 'Admin' : 'User'}`;

Try it Yourself »


Tagged Templates

You can also use template strings with a function (called a tag) to modify the output.

Note: Tagged templates are an advanced feature. You might not need them in most cases.

The function takes the text and the expression(s) as arguments.

Look at the example below:

Example

Tagged Template:

function highlight(strings, fname) {
  let x = fname.toUpperCase();
  return strings[0] + x + strings[1];
}

let name = "John";

let text = highlight`Hello ${name}, how are you?`;

Try it Yourself »

Example Explained

The function name is highlight, you can name it whatever you want.

The first argument holds the text between the expressions, as an array.
The array items are splitted by the expression.
In this example strings[0] holds "Hello " and strings[1] holds " how are you?".

The second argument holds the expressions. In this example fname holds "John".

Inside the function you can use the arguments to create the final string, and return it.

Note: If the template string contains multiple expressions, the text will still be held in the first argument, but the expressions will either be held in multiple arguments, or as an array in the second argument.

Example

Tagged Template with multiple expressions:

function highlight(strings, fname1, fname2) {
  let x = fname1.toUpperCase();
  let y = fname2.toUpperCase();
  return strings[0] + x + strings[1] + y + strings[2];
}

let name1 = "John";
let name2 = "Jane";

let text = highlight`Hello ${name1} and ${name2}, how are you?`;

Try it Yourself »

Example

Tagged Template with multiple expressions that are held in an array using the rest parameter:

function highlight(strings, ...fname) {
  let x = fname[0].toUpperCase();
  let y = fname[1].toUpperCase();
  return strings[0] + x + strings[1] + y + strings[2];
}

let name1 = "John";
let name2 = "Jane";

let text = highlight`Hello ${name1} and ${name2}, how are you?`;

Try it Yourself »



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.