JavaScript Multiline String

luca picture luca · Mar 22, 2011 · Viewed 14.5k times · Source

The question is:

What is the JavaScript method to store a multiline string into a variable like you can in PHP?

Answer

Martijn picture Martijn · Mar 22, 2011

If by 'multiline string' you mean a string containing linebreaks, those can be written by escaping them using \n (for newline):

var multilineString = 'Line 1\nLine 2';
alert(multilineString);
// Line 1
// Line 2

If you mean, how can a string be written across multiple lines of code, then you can continue the string by putting a \ backslash at the end of the line:

var multilineString = 'Line \
1\nLine 2';
alert(multilineString);
// Line 1
// Line 2