Wednesday, 28 March 2018

Creating multiline strings in JavaScript



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

Answer



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:



var html = `

Some HTML here

`;



(Note: I'm not advocating to use HTML in strings)



Browser support is OK, but you can use transpilers to be more compatible.






Original ES5 answer:



Javascript doesn't have a here-document syntax. You can escape the literal newline, however, which comes close:




"foo \
bar"

No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...