canufeel canufeel - 1 month ago 12
Javascript Question

Long, single line ES6 string literal

The google is full of blog posts and answers to question how one should benefit from ES6 string literals. And almost every blog post explaining this feature in depth has some details on how to implement multiline strings:

let a = `foo
bar`;


But i can not find any details on how to implement long single line strings like the following:

let a = `This is a very long single line string which might be used to display assertion messages or some text. It has much more than 80 symbols so it would take more then one screen in your text editor to view it. Hello ${world}`


Any clues or workarounds or should a one stick to the es3 strings instead?

Gpx Gpx
Answer

You can go on a new line with a string using \. The newline will not appear in the string itself.

let a = `This is a very long single line string which might be used \
to display assertion messages or some text. It has much more than \
80 symbols so it would take more then one screen in your text \
editor to view it. Hello ${world}`

Pay attention not to indent your string though or the indentation will be in the string:

let a = `Hello\
         World`;

a === 'Hello         World' // true
Comments