Using multiline string variables in JavaScript
Javascript never had good ways to use multiline strings before ES6 had appeared. In ES6 you can use template literals – strings delimited by backticks instead of quotes, single or double. But what to do if you can not use ES6 possibilities?
In ES6 multiline string value can be written with backticks:
const multiline = `this
is
a
multiline
string`;
But in classic Javascript you can also use multiline values. But it is not so comfortable to use as with ES6.
There are 2 ways how to do it. The first one is to escape newlines:
var multiline = 'This \
is\
a \
multiline \
string';
This method is not recommended. The second way, which is s recommended one, is to concatenate strings:
var multistring = 'This' +
'is ' +
'a new ' +
'multiline ' +
'string';
Don’t forget to write empty symbol (whitespace) before quotes if you want to have spaces between words.