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

161
Visualizações
Concatenate different variable if exist in JS

I want to concatenate different variable if exist and return the result. But I need to separate the value by "-" and return only the variable with value.

So I have this:

  var test = "";
  var test1 = "";
  var test2 = "";
  var test3 = "";

  var checkTrue = "Rightt";
  var checkFalse = "Wrong";
  var checkWord = "Helloo";
  var checkWord2 = "Bye";

  if(checkTrue === "Right"){
    test = "This is right"
  } else {
    test = "";
  }

  if(checkFalse === "Wrong"){
    test1 = "This is wrong"
  } else {
    test1 = "";
  }

  if(checkWord === "Hello"){
    test2 = "The word is hello"
  } else {
    test2 = "";
  }

  if(checkWord2 === "Bye"){
    test3 = "The word is hello"
  } else {
    test3 = "";
  }

  var result = test + " - " + test1 + " - " + test2 + " -" + test3;
  console.log(result);

So I think I need to verify before my var result, if the different variable exists ?

Thank for your help

Actual result:

 - This is wrong -  -The word is hello

Expected result :

This is wrong - The word is hello
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

With anything where you want a separator between string values, I find it easier to use Array.prototype.join() which will only place the separator between actual values.

It then becomes easier as you have a single results array that can be joined, only adding values you want rather than testing each of those variables you no longer need.

By pushing on to an array you can also get rid of all those test variables too.

const results = [];

const checkTrue = "Rightt";
const checkFalse = "Wrong";
const checkWord = "Helloo";
const checkWord2 = "Bye";

if (checkTrue === "Right")
  results.push("This is right")

if (checkFalse === "Wrong")
  results.push("This is wrong")

if (checkWord === "Hello")
  results.push("The word is hello")

if (checkWord2 === "Bye")
  results.push("The word is hello")

const result = results.join(" - ");
console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

const test = '';
const test1 = 'This is wrong';
const test2 = '';
const test3 = 'The word is hello'

const result = [test, test1, test2, test3].filter(value => value).join(' - ');
console.log(result);

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