Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

262
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda