I have the following code in Ruby. I want to convert this code into JavaScript. what's the equivalent code in JS?
text = <<"HERE"
This
Is
A
Multiline
String
HERE
I have the following code in Ruby. I want to convert this code into JavaScript. what's the equivalent code in JS?
text = <<"HERE"
This
Is
A
Multiline
String
HERE
the pattern
text = <<"HERE" This Is A Multiline String HERE
is not available in js (I remember using it much in my good old Perl days).To keep oversight with complex or long multiline strings I sometimes use an array pattern:
or the pattern anonymous already showed (escape newline), which can be an ugly block in your code:
Here's another weird but working 'trick'1:
external edit: jsfiddle
ES20xx supports spanning strings over multiple lines using template strings:
1 Note: this will be lost after minifying/obfuscating your code
ES6 Update:
As the first answer mentions, with ES6/Babel, you can now create multi-line strings simply by using backticks:
Interpolating variables is a popular new feature that comes with back-tick delimited strings:
This just transpiles down to concatenation:
Original ES5 answer:
I'm surprised I didn't see this, because it works everywhere I've tested it and is very useful for e.g. templates:
Does anybody know of an environment where there is HTML but it doesn't work?
The equivalent in javascript is:
Here's the specification. See browser support at the bottom of this page. Here are some examples too.
to sum up, I have tried 2 approaches listed here in user javascript programming (Opera 11.01):
So I recommend the working approach for Opera user JS users. Unlike what the author was saying:
It DOES work in Opera 11. At least in user JS scripts. Too bad I can't comment on individual answers or upvote the answer, I'd do it immediately. If possible, someone with higher privileges please do it for me.
My extension to https://stackoverflow.com/a/15558082/80404. It expects comment in a form
/*! any multiline comment */
where symbol ! is used to prevent removing by minification (at least for YUI compressor)Example: