Javascript heredoc

VeroLom picture VeroLom · Dec 7, 2010 · Viewed 59.9k times · Source

I need something like heredoc in JavaScript. Do you have any ideas for this? I need cross-browser functionality.

I found this:

heredoc = '\
<div>\
    <ul>\
        <li><a href="#zzz">zzz</a></li>\
    </ul>\
</div>';

I think it will work for me. :)

Answer

mko picture mko · Jan 10, 2015

Try ES6 String Template, you can do something like

var hereDoc = `
This
is
a
Multiple
Line
String
`.trim()


hereDoc == 'This\nis\na\nMultiple\nLine\nString'

=> true

You can use this great feature even in older browsers with TypeScript