How to create a multi-line string in JavaScript (and Observable)

While this post is triggered by working with Observable the solution works for Javascript ES6+.

I use Observable to connect to the AWS pricing data, stored on a PostgreSQL instance on AWS. So far so good, Observable allows for easy connection to databases.

When trying to write the code to query the database from Javascript

data = db.query("SELECT * 
FROM amazonec2
LIMIT 10
")

I would get the error message “Unterminated string constant”.

One solution is to put the string on one line.

data = db.query("SELECT * FROM amazonec2 LIMIT 10 ")

But when the query is long it is so much better to have formatted correctly.

Solution

Instead of " to open and close the string use ` (the backtick). Et voila.

data = db.query(`SELECT * 
FROM amazonec2
LIMIT 10
`)
comments powered by Disqus