/

How to Create a Multiline String in JavaScript

How to Create a Multiline String in JavaScript

Learn how to efficiently create multiline strings in JavaScript using template literals.

JavaScript lacked a proper way to handle multiline strings until the introduction of ES6 in 2015, along with the concept of template literals.

Template literals are strings enclosed in backticks (`), rather than the commonly used single or double quotes. They have a distinctive feature: they support multiline strings.

Here’s an example:

1
2
3
4
5
6
7
8
9
10
11
12
const multilineString = `A string
on multiple lines`;

const anotherMultilineString = `Hey
this is cool
a multiline
st
r
i
n
g
!`;

Using the backticks allows you to create multiline strings easily by pressing the Enter key. This can be particularly useful when dealing with lengthy strings or formatting text in a structured manner.

Tags: JavaScript strings, multiline strings, template literals