Multi-Line JavaScript Strings

How to declare a string over multiple lines in jQuery. It’s actually plain JavaScript and can be done by three difference ways.

  • Regular concatenation
  • Simply adding the escape character backslash after each line.
  • PERL style EOF with ES6 style variable interpolation.


Regular concatenation

The JavaScript language performs automatic semicolon insertion at the end lines, so creating multiline strings usually ends up looking something like this:


Simply adding the escape character backslash after each line

String upon string of concatenated JavaScript mess…ugly, slow, and …ugly. Many novice JavaScript developers don’t know that there’s a better way to create multiline strings:

Adding a backslash at the end of each line tells the JavaScript engine that the string will continue to the next line, thus avoiding the automatic semicolon insertion annoyance. Note that the second string includes line breaks within the string itself.


PERL style EOF with ES6 style variable interpolation

Is there a command that will read in text like the perl style EOF? For example:

In JavaScript,

More specifically – I have a multiline string variable in PERL that I am trying to pass to JavaScript. Problem is that it has \n and \r and some specific formatting that I would like to preserve. JavaScript keeps giving me “Unterminated String Constant” error. I have tried parsing out the \n and \r using ES6 style variable interpolation then the string is readable on web page.


Prakash S

Prakash S

I would like to introduce myself as a Software professional opting for the career in software industry. I'm Prakash S, a MCA graduate and trained as industry level practice for Software technology. Basically I am a PHP Developer but now days exploring more in HTML5, CSS, AngularJS and jQuery libraries.