Building a multi-line string in javascript with template literals

2.5k views Asked by At

I'm trying to build a URL with multiple query parameters using template literals.

I am putting each query parameter on a separate line for readability sake. It looks like the following example, only a lot longer.

const url = `http://example.com/hey?
one=${one}&
two=${two}&
three=${three}`;

My question deals with making a multi-line literal string that has a final value without the newline (\n) character in-between each parameter. Is that possible with template literals, or should I just concatenate the string the old way?

1

There are 1 answers

1
Nina Scholz On BEST ANSWER

You could use backslash at the end of the line for template literals, with more lines, but without a line break.

const 
    one = 'eins', two = 'zwei', three = 'drei',
    url = `http://example.com/hey?\
one=${one}&\
two=${two}&\
three=${three}`;

console.log(url);