What is a template literal in JavaScript — and why a pair of parentheses opens an SQL injection

Look at these two lines of code. They differ by one pair of parentheses:
await sql`SELECT * FROM users WHERE id = ${id}`
await sql(`SELECT * FROM users WHERE id = ${id}`)
The first is safe. The second is a classic SQL injection: if id arrives as 1 OR 1=1, the query returns every user. Depending on the library, the second line either throws an error or silently opens a hole.
Why does one pair of parentheses change so much? To see it, you need to understand template literals. People write them every day, but few know their second half.
What a template literal is
A template literal is a string in backticks `. On most keyboards the backtick sits to the left of the 1 key.
Inside you can insert any expression with ${...}:
const name = 'Anna'
const total = 3
`Hi, ${name}! You have ${total} items in your cart`
// 'Hi, Anna! You have 3 items in your cart'
Any JavaScript works inside ${}: ${price * 2}, ${user.name.toUpperCase()}, ${isAdmin ? 'admin' : 'guest'}. The result is turned into a string and dropped in place.
The second superpower is line breaks. Regular quotes can't do it, backticks can:
const message = `Order received.
A courier is on the way.`
More on strings in general in what is a string.
Two everyday traps
Regular quotes instead of backticks. The most common mistake:
'Hi, ${name}' // literally prints: Hi, ${name}
Interpolation works only in backticks. In single or double quotes ${name} is just characters. No error — just broken text on screen. ESLint has a no-template-curly-in-string rule that catches exactly this.
The wrong thing gets inserted. A template silently turns anything into a string:
`${user}` // '[object Object]'
`${[1, 2, 3]}` // '1,2,3'
`${undefined}` // 'undefined'
See "Hi, undefined" or "[object Object]" on the page? Look for a template literal that received the wrong type. More on such values in the article on null and undefined.
And a small thing about line breaks. Leading spaces end up in the result too. If an indented template lives inside a function, the indentation lands in the text of your email or prompt.
Tagged templates — the second half
You can put a function name right before the backticks, without parentheses. That's a tagged template:
tag`Hi, ${name}! You are ${age}`
JavaScript does not glue this string together. It calls tag and passes the parts separately:
tag(['Hi, ', '! You are ', ''], name, age)
First argument — an array of text pieces. Then the values, each on its own. The function decides what to do with them. It can return a string, an object, or a database query.
A built-in example is String.raw. It doesn't process backslashes:
String.raw`C:\new\folder` // 'C:\new\folder' — \n did not become a line break
Back to SQL
Now the difference between the two lines from the start is visible.
sql`... ${id}` is a tagged template. The library receives the query text and the id value separately. The value goes to the database as a parameter. The database will never execute it as a piece of SQL. Whatever arrives in id, it's just data.
sql(`... ${id}`) builds an ordinary string first. The id value is pasted into the query text. Only then is the finished string passed to the function. Telling your SQL apart from SQL pasted in by an attacker is no longer possible.
Many tools work this way:
- Postgres drivers like
postgresand@neondatabase/serverless—sql`...`; - Prisma:
$queryRaw`...`is safe, while$queryRawUnsafe(...)with a template inside isn't; - HTML templates like
html`...`in the Lit library escape values and protect against XSS.
The same logic explains a second, similar hole: element.innerHTML = `<b>${name}</b>`. An ordinary template literal escapes nothing. If name came from a user, it may contain a script. More on SQL as a language in what is SQL.
How to check AI-written code
AI models love building queries with a template literal — it's shorter. Before you deploy, search your project for:
query(,execute(,raw(with backticks inside the parentheses;Unsafein method names;innerHTML =with a template literal on the right.
Each hit is a hole candidate. Ask the AI to rewrite it as a tagged template or with query parameters.
How is a template literal different from a regular string?
It's written in backticks, can insert values with ${} and keep line breaks. In regular quotes ${} doesn't work and is printed as text.
Can template literals be nested?
Yes: inside ${} you can write another template literal. But it's hard to read. Better pull that piece into a variable.
Are template literals slower than plain concatenation?
In practice, no. The difference gets lost in the noise. Pick whatever reads better.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





