JavaScript Multiline String: Creating Multi-Line Strings in JavaScript

Estimated read time 3 min read

Introduction to Multiline Strings in JavaScript

In JavaScript, strings are typically enclosed in single or double quotes and can span only a single line. However, there are scenarios where you may need to work with multiline strings that span multiple lines. Multiline strings allow you to include line breaks and preserve the formatting and structure of the text. In this article, we will explore different techniques for creating multiline strings in JavaScript, providing you with the knowledge to handle complex text content effectively.

Using Template Literals for Multiline Strings

Introduction to Template Literals

Template literals, introduced in ECMAScript 2015 (ES6), are a powerful feature in JavaScript that allows the creation of multiline strings with ease. Template literals are denoted by backticks (`) instead of single or double quotes.

Creating Multiline Strings with Template Literals

To create a multiline string using template literals, you can simply include line breaks within the backticks. Here’s an example:

const multilineString = `
  This is a
  multiline string
  created with
  template literals.
`;

console.log(multilineString);

In this example, the string is defined using backticks and includes line breaks. When the multilineString variable is printed, the output will preserve the line breaks, resulting in a multiline string.

Interpolating Variables in Multiline Strings

One of the advantages of template literals is the ability to interpolate variables directly within the multiline string. This allows you to dynamically insert values into the string. Here’s an example:

const name = 'John';
const age = 30;

const greeting = `
  Hello, my name is ${name}.
  I am ${age} years old.
`;

console.log(greeting);

In this example, the variables name and age are interpolated within the multiline string using the ${} syntax. The output will display the values of the variables within the string.

Using String Concatenation for Multiline Strings

Concatenating Strings

Another approach to create multiline strings in JavaScript is by concatenating multiple strings together using the + operator. Each string can be on a separate line, allowing you to achieve the desired multiline structure. Here’s an example:

const multilineString =
  'This is a ' +
  'multiline string ' +
  'created by concatenating ' +
  'multiple strings.';

console.log(multilineString);

In this example, each line is represented as a separate string, and they are concatenated using the + operator. The resulting string will preserve the multiline structure when printed.

Escaping Line Breaks

When using string concatenation, it’s important to note that JavaScript automatically removes line breaks. To include line breaks in the concatenated string, you need to manually add escape characters (\n) for line breaks. Here’s an example:

const multilineString =
  'This is a \n' +
  'multiline string \n' +
  'with escaped line breaks.';

console.log(multilineString);

In this example, the escape character \n is added at the end of each line to signify a line break. When printed, the output will include the desired line breaks.

Using Array Join Method for Multiline Strings

Array Join Method

The join() method in JavaScript allows you to concatenate the elements of an array into a single string. By using this method, you can create multiline strings by storing each

Angelika Card

Hi all, my name is Angelika and I am one of the authors of the EasyTechh website. Like the rest of our team I am incredibly ambitious and I love helping people.
That's why I write here and not only here ;-) I write interesting and useful for people articles in the IT sphere and a little bit about life.
Enjoy reading.

You May Also Like

More From Author

+ There are no comments

Add yours