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?
|
|||||||||||||
|
Javascript doesn't have a here-document syntax. You can escape the literal newline, however, which comes close:
Update:ECMAScript 6 (ES6) introduces a new type of literal, namely template literals. They have many features, variable interpolation among others, but most importantly for this question, they can be multiline. A template literal is delimited by backticks:
(Note: I'm not advocating to use HTML in strings) Browser support is OK, but you can use transpilers to be more compatible. |
|||||||||||||||||||||
|
Google's JavaScript style guide recommends to use string concatenation instead of escaping newlines because the latter isn't a part of ECMAScript:
UpdateIn EcmaScript 6, you'll be able to use backticks for Template Strings, known in the spec as a NoSubstitutionTemplate:
I think you can try this and other features by first downloading canary chrome and then turning on |
|||||||||||||||||||||
|
the pattern 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 [addition 2015]
1 Note: this will be lost after minifying/obfuscating your code |
|||||||||||||||||||||
|
You can have multiline strings in pure JavaScript. This method is based on the serialization of functions, which is defined to be implementation-dependent. It does work in the most browsers (see below), but there's no guarantee that it will still work in the future, so do not rely on it. Using the following function:
You can have here-documents like this:
The method has successfully been tested in the following browsers (not mentioned = not tested):
Be careful with your minifier, though. It tends to remove comments. For the YUI compressor, a comment starting with I think a real solution would be to use CoffeeScript. |
|||||||||||||||||||||
|
You can do this...
|
|||||||||||||||||||||
|
I came up with this very jimmy rigged method of a multi lined string. Since converting a function into a string also returns any comments inside the function you can use the comments as your string using a multilined comment /**/. You just have to trim off the ends and you have your string.
|
|||||||||||||||||||||
|
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? |
|||||||||||||||||||||
|
I solved this by outputting a div, making it hidden, and calling the div id by jQuery when i needed it. e.g.
Then when i need to get the string, i just use the following jQuery:
Which returns my text on multiple lines. If i call
I get: |
|||||||||||||||||||||
|
I like this syntax and indendation:
(but actually can't be considered as multiline string) |
|||||||||||||||||
|
Using script tags:
|
|||||||||||||||||
|
There's this library that makes it beautiful: https://github.com/sindresorhus/multiline Before
After
|
|||||||||||||||||||||
|
There are multiple ways to achieve this 1. Slash concatenation
2. regular concatenation
3. Array Join concatenation
Performance wise, Slash concatenation (first one) is the fastest. Refer this test case for more details regarding the performance |
|||||
|
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. |
|||||||||
|
Downvoters: This code is supplied for information only. This has been tested in Fx 19 and Chrome 24 on Mac
|
|||||||||||||||||||||
|
Updated for 2015: it's six years later now: most people use a module loader, and the main module systems each have ways of loading templates. It's not inline, but the most common type of multiline string are templates, and templates should generally be kept out of JS anyway. require.js: 'require text'.Using require.js 'text' plugin, with a multiline template in template.html
NPM/browserify: the 'brfs' moduleBrowserify uses a 'brfs' module to load text files. This will actually build your template into your bundled HTML.
Easy. |
||||
|
If you're willing to use the escaped newlines, they can be used nicely. It looks like a document with a page border. |
|||||||||
|
This works in IE, Safari, Chrome and Firefox:
|
|||||||||||||||||
|
My extension to http://stackoverflow.com/a/15558082/80404.
It expects comment in a form
Example:
|
||||
|
You can use TypeScript (JavaScript SuperSet), it supports multiline strings, and transpiles back down to pure JavaScript without overhead:
If you'd want to accomplish the same with plain JavaScript:
Note that the iPad/Safari does not support If you have a lot of legacy code, you can also use the plain JavaScript variant in TypeScript (for cleanup purposes):
and you can use the multiline-string object from the plain JavaScript variant, where you put the templates into another file (which you can merge in the bundle). You can try TypeScript at |
||||
|
The equivalent in javascript is:
Here's the specification. See browser support at the bottom of this page. Here are some examples too. |
||||
|
My version of array-based join for string concat:
This has worked well for me, especially as I often insert values into the html constructed this way. But it has lots of limitations. Indentation would be nice. Not having to deal with nested quotation marks would be really nice, and just the bulkyness of it bothers me. Is the .push() to add to the array taking up a lot of time? See this related answer: (Is there a reason JavaScript developers don't use Array.push()?) After looking at these (opposing) test runs, it looks like .push() is fine for string arrays which will not likely grow over 100 items - I will avoid it in favor of indexed adds for larger arrays. |
||||
|
You can use
can be also written as
|
|||
|
I think this workaround should work in IE, Chrome, Firefox, Safari, Opera - Using jQuery :
Using Pure Javascript :
Cheers!! |
|||||||||||||
|
Just tried the Anonymous answer and found there's a little trick here, it doesn't work if there's a space after backslash
But when space is removed it works -
Hope it helps !! |
|||||
|
i found a more elegant solution, maybe a little non-topic related because it uses PHP, but im sure it will be useful and cuteness* for some of yours... this javascript code should stay inside script tags
in your output html you will see something like
and et voilà!, it gives you code readability in your file. pD: this sample uses json_encode() PHP function, but certainly there are function equivalents for ASP, Ruby and JSP langs. pD: however, this solution have his limitations too, one of them is you cannot use javascript variables inside the encapsulated code. |
|||||||||
|
If you happen to be running in Node only, you could use the fs module to read in the multi-line string from a file:
|
|||
|
This is one fairly economical approach, at least in terms of the source code:
would be nicer of course if the "arguments" property were a proper array. A second version might use "" to do the join for cases when you want to control the line breaks in a very long string. |
|||||
|
I program this way:
|
|||||||||
|
I think I discovered another way to do it inline without any invasive syntax on every line. Use Javascript's ability to convert a function to string and create a multiline comment with the
The only pitfall I can see in this is the syntax highlighting. EDIT: Had I scrolled down a little more, I would have seen this answer doing exactly the same thing: http://stackoverflow.com/a/5571069/916553 |
||||
|
It's not extremely elegant but it's clean enough for me:
|
|||||||||||||||||||||
|
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?