How to Create a Multi-Line String in JavaScript?

Estimated read time 1 min read

In JavaScript, you can create a multi-line string by using a combination of escape characters and string concatenation. Here are two common methods for creating multi-line strings in JavaScript:

  1. Using template literals:
let multilineString = `This is a
multi-line string
in JavaScript`;

console.log(multilineString);

In this example, template literals are used to create a multi-line string. The string is defined using backticks (`) instead of quotes (' or "), and line breaks are automatically included in the final string.

  1. Using string concatenation:
let multilineString = "This is a\n" +
                      "multi-line string\n" +
                      "in JavaScript";

console.log(multilineString);

In this example, a multi-line string is created by concatenating multiple strings using the + operator. The \n escape character is used to insert a line break between each string.

Both of these methods allow you to create multi-line strings in JavaScript, and you can choose the one that best fits your needs and coding style.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply