Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

260
Views
Pass the value of external variables in a tagged template

tag is a function that accepts a template string:

function tag(strings) {
    console.log(strings.raw);
}

tag`Output something`

It works fine. Now the problem is, I have an external variable and I want to pass the value of the variable to the template string, something like this:

function tag(strings) {
    console.log(strings.raw);
}

const key = "a"
tag`The value of key: ${key}` // I need it to output "The value of key: a", but the actual output is "The value of key: "

It doesn't work as expected because javascript thinks key is one of tag's parameters. How to solve this problem?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to have another parameter that accepts the key value.

function tag( strings, key ) {
    console.log(strings[0] + key);
}

const key = "a"
tag`The value of key: ${key}`
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!