liveBook · Manning

Multiline string

As explained in Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond

A string literal form whose source text may span multiple lines, preserving line breaks and other whitespace in the literal text.

Need readable text or markup across several lines? A JavaScript template literal uses backticks and lets the literal itself span lines. The line break becomes part of the string; unlike a quoted string containing `\n`, the source is written on multiple lines. section 7.1

A line break in the literal

`Hello,\nWorld` written with an actual line break between “Hello,” and “World” produces a string containing that line break. section 7.1

function getProductHTML(product) {
  return `
    <div class="product">
      <div class="product-image">
        <img alt="${product.name}" src="${product.image_url}">
      </div>
      <div class="product-desc">${product.desc}</div>
    </div>
  `;
}

See a product HTML template laid out across lines, with product values interpolated into the markup. section 7.1

How Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond covers it

How deep it goes
Defines multiline literals, distinguishes them from newline escapes, and demonstrates the resulting line break without exploring implementation internals.
Examples
Shows a two-line greeting and product HTML formatted across multiple lines.
What you can do after
Apply template literal syntax to write strings that span source lines, including readable HTML markup.
How it connects
Connects multiline strings to template literals and string interpolation.

Also explained in

Compare how 3 books explain multiline string →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond. The section links point to the parts of the book it draws on.