Question #512   Submitted by Answiki on 07/12/2021 at 05:15:30 PM UTC

How to create multi-line strings in JavaScript?

Answer   Submitted by Answiki on 07/12/2021 at 05:23:26 PM UTC

There are several ways to create multi-line strings in JavaScript:


With ES6, the simplest option is to use template literals:

var str = `This is a string
on several lines`;

With ES5, new lines can be escaped with \:

var str = 'This is a string \
on several lines';

Google JavaScript Style Guide recommends using string concatenation instead escaping new lines:

var str = 'This is a string ' +
	'on several lines';

3 events in history
Question by Answiki 07/12/2021 at 05:24:02 PM
In JavaScript, how to create a string on several lines?
Answer by Answiki on 07/12/2021 at 05:23:26 PM

There are several ways to create multi-line strings in JavaScript:


With ES6, the simplest option is to use template literals:

var str = `This is a string
on several lines`;

With ES5, new lines can be escaped with \:

var str = 'This is a string \
on several lines';

Google JavaScript Style Guide recommends using string concatenation instead escaping new lines:

var str = 'This is a string ' +
	'on several lines';

Question by Answiki 07/12/2021 at 05:15:30 PM
How to create multi-line strings in JavaScript?
# ID Query URL Count

Icons proudly provided by Friconix.