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

132
Visualizações
Which conditionals would you prefer

Which way of defining a string would you use?

Are there better ways to do this with even more strings?

I want to set abbreviations for a large set of predefined strings.

If style 1:

let stat = str3;

if (stat === "str1") {
    stat = "string1";
} else if (stat === "str2") {
    stat = "string2";
} else if (stat ==="str3") {
    stat = "string3";
} else if (stat === "str4") {
    stat = "string4";
} else if (stat === "str5") {
    stat = "string5";
}

If style 2: (I think this is the best visually)

let stat = str3;

if (stat === "str1") stat = "string1";
else if (stat === "str2") stat = "string2";
else if (stat === "str3") stat = "string3";
else if (stat === "str4") stat = "string4";
else if (stat === "str5") stat = "string5";

Switch style:

let stat = str3;

switch (stat) {
case "str1":
    stat = "string1";
    break;
case "str2":
    stat = "string2";
    break;
case "str3":
    stat = "string3";
    break;
case "str4":
    stat = "string4";
    break;
case "str5":
    stat = "string5";
    break;
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Put all the data in an object and then just access the values by key.

const dict = {
  str1: 'string1',
  str2: 'string2'
};

const stat = 'str1';

console.log(dict[stat]);

about 4 years ago · Juan Pablo Isaza Relatório

0

In my opinion you should use Switch because it's faster than "if" and also if it gets bigger it will be easier to read.

A switch statement might prove to be faster than ifs provided number of cases are good. If there are only few cases, it might not effect the speed in any case. Prefer switch if the number of cases are more than 5 otherwise, you may use if-else too

You could read More in this link

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