javascriptecmascript-6template-literals

How can I call a template literal tag function on a regular string?


If I have a template literal tag function foo, which let's me do:

const fooTaggedText = foo`some text`;

is it possible for me to somehow call that tag on a regular string? For instance:

 // This doesn't actually work
 const fooTaggedText = foo('some text');

Solution

  • I'm not sure if I got it, but wouldn't this work?

    const taggedText = str => foo`${str}`;
    

    So you'd use it like:

    taggedText('my string')
    
    // foo`my string`
    

    Please give some rep if it helps :)