Search code examples
javascriptstringmultilineheredoc

JavaScript Multiline String


The question is:

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


Solution

  • 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