r/ProgrammerTIL • u/ephemat234 • Oct 02 '23
Javascript TIL JavaScript provides string interpolation with string template literals
I started learning JavaScript in 2012 before this was full supported, never realized the backtick was used in JavaScript. It works great
const number = 2
const message = `The number is ${number}`
console.log(message); // => 'The number is 2'
10
Upvotes
View all comments
Show parent comment
1
u/bloody-albatross Oct 08 '23
There are certain SQL query builder that use them so that any
${substitution}gets correctly escaped (or replaced with?for a prepared statement). That is handy. Since the query function only allows the return value of that special template string (which isn't a string) it forces you to use that and you can't pass a normal string as a query.