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

107
Vistas
Refactoring the ternary operator

I am quite stuck in a refactored ternary operator while learning react. Here is the code I came across inside a JSX:

{props.openSpots === 0 && <div className="card--badge">SOLD OUT</div>}

I would like to ask why this will return the card--badge div? I am only familiar with the complete common ternary operator that looks like this instead:

{props.openSpots === 0 ? <div className="card--badge">SOLD OUT</div> : null}

Thank you very much.

about 4 years ago · Santiago Gelvez
2 Respuestas
Responde la pregunta

0

AND operator looks for the second argument if the first argument is true.

But, if first argument is false it does not look for second argument, and directly returns false.

true && statement == statement;

false && statement == false;

console.log(
    true && "I am visible"
)
console.log(
   '  ' && "I am visible"
)

console.log(
    false && "I am not visible"
)
console.log(
    0 && "I am not visible"
)
console.log(
   null && "I am not visible"
)
console.log(
   undefined && "I am not visible"
)

console.log(
   true && '' && "I am not visible"
)

Link for reference

about 4 years ago · Santiago Gelvez Denunciar

0

From the react documentation:

It works because in JavaScript, true && expression always evaluates to expression, and false && expression always evaluates to false.

Therefore, if the condition is true, the element right after && will appear in the output. If it is false, React will ignore and skip it.

See: https://reactjs.org/docs/conditional-rendering.html#inline-if-with-logical--operator

about 4 years ago · Santiago Gelvez 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