Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Multiline String [duplicate]

The question is:

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

like image 428
luca Avatar asked Mar 22 '11 13:03

luca


2 Answers

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
like image 199
Martijn Avatar answered Oct 11 '22 14:10

Martijn


var es6string = `<div>
    This is a string.
</div>`;

console.log(es6string);
like image 37
Endless Avatar answered Oct 11 '22 15:10

Endless