Whilst “\n” is the normal way of adding a newline to a JavaScript string (or “\r\n”), if you do it inside a php heredoc string, it gets stripped and turned into a newline in the output, so its lost from the JavaScript string and the browser see’s the string continue on a new line causing a JavaScript error.

You can use \\n:

  $HtmlOutput .= <<<_END
    <script>
      const text = "The first line of my string:\\nThe second line of my string";
    </script>
_END;

You can also use fromCharCode to solve this:

  $HtmlOutput .= <<<_END
    <script>
      const text = "The first line of my string:" + String.fromCharCode(10) + "The second line of my string";
    </script>
_END;