Skip to Content

How to Create Multiline Strings in JavaScript

This tutorial will show you how to create clean multiline strings in JavaScript, making your code more readable.

The Dark Times

Before 2015, when JavaScript ES6 was introduced to the world, the only way you could write multiline strings was on one string per line with opening and closing quotations followed by a plus sign + to group them together:

var my_string =
"This is the first line." +
"This is the second line." +
"This is the third line.";

As you can see, this method is a bit of a mess and is tough to read if you have long or complicated strings.

Multiline Strings with Template Literals

Template literals are strings delimited by adding a backquote, or backtick, ` character to the beginning and end of your string. With this new rule in place, you're now able to separate your string over multiple lines without the use of excess quotes:

var my_string = `
This is the first line.
This is the second line.
This is the third line.
`;

Conclusion

Multiline strings are a great way to keep your JavaScript code clean and easily readable.

Posted by: Josh Rowe
Created: August 13, 2022

Comments

There are no comments yet. Start the conversation!

Add A Comment

Comment Etiquette: Wrap code in a <code> and </code>. Please keep comments on-topic, do not post spam, keep the conversation constructive, and be nice to each other.