Saturday, 1 February 2014

Creating multiline strings in JavaScript



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:
var myString = 
['<div id="someId">',
'some content<br />',
'<a href="#someRef">someRefTxt</a>',
'</div>'
].join('\n');
or the pattern anonymous already showed (escape newline), which can be an ugly block in your code:
    var myString = 
'<div id="someId"> \
some content<br /> \
<a href="#someRef">someRefTxt</a> \
</div>'
;

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More