Tagged Templates

by Rohit S,

We are already aware of template literal as shown below:

const name = 'Groot';
console.log(`I am ${name}`);

When we use template literal, any JavaScript expression is valid within the curly braces {}.

So, What are tagged template literals ?

function blackBox(strings, name) {
  console.log(strings, name);
}

const name = 'Groot';
blackBox`I am ${name}!`;

The output of the above code snippet is:

Array ["I am ","!"] Groot

The first argument of blackBox function is an array of strings and second argument is the resolved value of the first expression we passed.

But we can have n number of template interpolation, for example:

const blackBox = (strings, ...args) => {
  console.log('Array of strings:', strings);
  console.log('All expression values:', args);
};

const firstName = 'Rohit';
const lastName = 'S';
const age = 200;

blackBox`I am ${firstName}, ${lastName} and ${age}.`;

NOTE: Tagged templates works well with ES6 arrow function declaration.

The output of the above code snippet is:

Array of strings: Array(4)[("I am ", ", ", " and ", ".")];
Array of args: Array(3)[("Rohit", "S", 200)];

Applications of tagged templates:

High-level implementation of styled-components:

function button(styles) {
  return function NewComponent(props) {
    const uniqueClassName = generateUniqueName(styles);
    const getstyles = processStylesThroughStylis(styles);
    createAndInjectCSSClass(uniqueClassName, getstyles);

    return <button className={uniqueClassName} {...props} />;
  };
}
const Button = styled.button`
  padding: 8px;
`;

function PrimaryButton() {
  return <Button variant="primary">Click Me</Button>;
}

For in-depth understanding of how tagged templates are used by styled-components please check the source code here source code

More articles

Protecting Your LLM Applications from Prompt Injection Attacks

Learn practical techniques to defend against prompt injection attacks in AI applications with simple code examples.

Read more

How to Read a Flame Graph in Chrome DevTools

A deep, practical guide to reading flame charts in Chrome DevTools, spotting expensive functions, and validating performance improvements.

Read more

Your competitors are already using AI.
The question is how fast you want to unlock the value?

Don't know where to start?

AI is everywhere but it's unclear which investments will actually move your metrics and which are expensive experiments.

Your data isn't ready

Most AI projects fail at the data layer. Pipelines, quality, access all need work before LLMs can deliver value.

Internal teams are stretched

Your engineers are shipping product. They don't have capacity to also become AI specialists with production-grade experience.

Legacy systems block everything

Aging, undocumented codebases make AI integration slow, risky, and expensive. They need to move first.

Don't Worry. We've got you covered.

Start with the audit.