Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

116
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda