Skip to main content

How to Put Variables in a String with Template Literals in JavaScript

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Building a message from variables with + gets messy fast: 'You ordered ' + quantity + ' ' + item + 's'. A template literal writes the whole message once and drops the values in where they belong.

Syntax​

Syntax
const <text> = `Plain text ${<expression>} more text`;
PartWhat it does
` (backticks)Start and end a template literal. Single or double quotes don't work.
${<expression>}A placeholder: JavaScript runs the expression and puts its result into the string.
<expression>A variable, a calculation or a function call.

MDN's template literals reference: "Template literals are literals delimited with backtick (`) characters, allowing for multi-line strings, string interpolation with embedded expressions, and special constructs called tagged templates."

The Example​

The example builds an order summary from an item name, a quantity and a price, and works out the total inside the string.

Prerequisites​

  • Node.js. This guide uses Node.js 26.13.1.

Step 1: Write the Code​

Create a file named main.js:

main.js, which sets item to coffee, quantity to 2 and price to 3.5, and prints a template literal that puts the quantity, the item and the quantity times the price into one sentence.

main.js
const item = 'coffee';
const quantity = 2;
const price = 3.5;

console.log(`You ordered ${quantity} ${item}s for ${quantity * price} euros.`);
  • ${quantity} and ${item} insert the variables' values.
  • ${item}s puts an s straight after the value, making coffees.
  • ${quantity * price} runs the calculation first and inserts 7.

Step 2: Run It​

Terminal
node main.js
Output
You ordered 2 coffees for 7 euros.

A zsh terminal where node main.js prints You ordered 2 coffees for 7 euros.

The command is the same in zsh, bash and PowerShell.

Text on Several Lines​

A template literal keeps the line breaks you type, so a message over two lines needs no \n:

Two lines
const name = 'Ada';
console.log(`Hello, ${name}!
Welcome back.`);
Output
Hello, Ada!
Welcome back.

Common Mistakes​

MistakeWhat happensDo this instead
Using single or double quotesThe placeholder is printed as it is: You ordered ${q} coffeesUse backticks
Leaving out the ${quantity} is printed as plain textWrite ${quantity}
Indenting the second line of a multi-line literalThe spaces become part of the stringStart continuation lines at the left edge, or join lines with \n

Troubleshooting​

SymptomLikely causeFix
${...} shows up in the outputThe string is in quotes, not backticksSwitch to backticks
undefined appears in the textA placeholder names a variable with no valueCheck the variable before building the string

FAQ​

Can I call a function inside the placeholder?​

Yes. Any expression works, such as ${name.toUpperCase()}.

How do I write a backtick inside a template literal?​

Escape it with a backslash: `a \` b` gives a ` b.

Conclusion​

Backticks make a template literal, and each ${...} inside drops in the result of an expression. It reads like the final text and also keeps line breaks.

Additional Resources​