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
Just tried the Anonymous answer and found there's a little trick here, it doesn't work if there's a space after backslash
\
So the following solution doesn't work -
But when space is removed it works -
Hope it helps !!
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.
ES6 allows you to use a backtick to specify a string on multiple lines. It's called a Template Literal. Like this:
Using the backtick works in NodeJS, and it's supported by Chrome, Firefox, Edge, Safari, and Opera.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
Please for the love of the internet use string concatenation and opt not to use ES6 solutions for this. ES6 is NOT supported all across the board, much like CSS3 and certain browsers being slow to adapt to the CSS3 movement. Use plain ol' JavaScript, your end users will thank you.
Example:
var str = "This world is neither flat nor round. "+ "Once was lost will be found";
I like this syntax and indendation:
(but actually can't be considered as multiline string)
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: