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

122
Vistas
How to add a part of code to the ternary operator using javascript and react?

i have code like below,

const variableType = 'INTEGER';
const editedValue = 100;
const defaultValue = 4;
return (
    isOpen ? (
        <span>something</span>
    ) : (
        <>
            {(variableType ?? '').toUpperCase() === 'BOOLEAN'
                ? capitalize(editedValue ?? '')
                : {editedValue}
        </>
    );

Now i want to add is when this variableType is not boolean and editedValue is not same as defaultValue i want to show an icon along with editedValue

if variableType is not boolean and edited Value is same as defaultValue i want to just show editedValue.

i have tried something like so,

return (
    isOpen ? (
        <span>something</span>
    ) : (
        <>
            {(variableType ?? '').toUpperCase() === 'BOOLEAN'
                ? capitalize(editedValue ?? '')
                : {editedValue !== defaultValue && (
                    <Icon />
                  }
                  {editedValue}
        </>
    );

But the above doesnt seem right syntatically.

how can i change the above ternary operator to satisfy above condition and show icon. could someone help me with this. i am new to using ternary operator. thanks.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The key here is understanding what parts are in JSX elements and what parts aren't. When you're in an element or fragment, you're in a JSX element and you use {...} to insert values or code. When you're inside {...}, you're not in a JSX expression.

I think you probably want:

return (
    isOpen ? (
        <span>something</span>
    ) : (
        (variableType ?? '').toUpperCase() === 'BOOLEAN'
            ? capitalize(editedValue ?? '')
            : <>
                {editedValue !== defaultValue && <Icon />}
                {editedValue}
              </>
    );

....but I would suggest you break that up to make it easier to read and maintain. For instance:

if (isOpen) {
    return <span>something</span>;
}
if ((variableType ?? '').toUpperCase() === 'BOOLEAN') {
    return capitalize(editedValue ?? '');
}
return <>
    {editedValue !== defaultValue && <Icon />}
    {editedValue}
</>;
about 4 years ago · Juan Pablo Isaza Denunciar

0

  if (isOpen) return <span>something</span>

  return (
    <>
      {(variableType ?? '').toUpperCase() === 'BOOLEAN'
        ? capitalize(editedValue ?? '')
        : <>
          {editedValue !== defaultValue && <Icon />}
          {editedValue}
        </>
      }
    </>
  )
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