63 ms·
Probably worth pointing out that this won't play nice with most minification utilities unless you don't mind leaving all comments in the output. I think for th
by wldlyinaccurate 12y ago
Probably worth pointing out that this won't play nice with most minification utilities unless you don't mind leaving all comments in the output.
I think for this reason I still prefer using Array.prototype.join for multiline strings:
var str = [
'<!doctype html>',
'<html>',
'<body>',
'<h1>❤ unicorns</h1>',
'</body>',
'</html>',
].join('');
- mofle 12y agoYou can use the `/*@preserve` directive. See: https://github.com/sindresorhus/multiline#minification https://github.com/sindresorhus/multiline#minification
- wldlyinaccurate 12y agoHey, thanks - that'll teach me for not reading the full page!
- gravity13 12y agoYou can also var str = ' \ <!doctype html> \ <html> \ <body> \ <h1>❤ unicorns</h1> \ </body> \ </html> \ ' I find it's better than string concat because the trailing \ is just one annoying thing rather than three.
- tekacs 12y ago... also this is (presumably) 'joined' at parse time, rather than runtime (and so only once per execution, guaranteed).
- jaredmcateer 12y ago6 months later: var str = ' \ <!doctype html> \ <html> \ <body> \ <h1>❤ unicorns</h1> \ </body> \ </html> \ ' Help I'm maintaining your code and I get a syntax error
- jasonkostempski 12y agoHA! I (don't) see what you did there :)
- msujaws 12y agoThere is a space following one of the slashes.
- andrey-p 12y agoOoh I really really like this. Preserving newlines (one of the stated usecases for multiline) is dead easy too. var str = [ //... ].join("\n");