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

117
Visualizações
Ternary Shorthand to Assign the Condition?

Is there any type of shorthand when using ternary assigns to say "return the condition?"

You use ternary in javascript as so:

const cityMayor = country?.state?.county?.city?.mayor
  ? country.state.county.city.mayor
  : "Open for vote";

Is there a way to say "use the condition?"

e.g. Does something like this exist:

const cityMayor = country?.state?.county?.city?.mayor
  ? <- 
  : "Open for vote";

To avoid having to retype everything in the true condition, return the condition as the true evolution?

I know I can write a method to do this for me, but is there anything built-in that does this?

I know I can do this, but I don't want to have to constantly import it:

const tAssign = (c, e) => {
  return c ? c : e;
}

const cityMayor = tAssign(country?.state?.county?.city?.mayor, "Open for vote");

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use null coalescing:

country?.state?.county?.city?.mayor ?? "Open for vote"

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use:

const item = country?.state?.county?.city?.mayor || "Vince McMahon";

But in some specific cases and frameworks, like React Native for example, it's strong recommended to avoid this, using the ternary operator (event it's not look to good) the avoid problems with rendering.

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