How to Create a Multiline String in JavaScript

It was an issue creating multiline strings in JavaScript until template literals were introduced in the ES2015 specification.

 

Template literals, also known as template strings allow embedded expressions including multi-line strings and string interpolation.

 

To make a template literal wrap the string in `` (backticks). Let's try out a couple of multiline strings and check what the output is.

 

var multiline_string = `hello this is a line
and this is a line
and another line`;

console.log(multiline_string);
hello this is a line
and this is a line
and another line
var letters = `a
b
c
d
e`;

console.log(letters);
a
b
c
d
e

 

Conclusion

Now you know how to use multiline strings in a JavaScript program without having to implement messy workarounds.

string line