Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

120
Views
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 answers
Answer question

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 Report

0

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

  return (
    <>
      {(variableType ?? '').toUpperCase() === 'BOOLEAN'
        ? capitalize(editedValue ?? '')
        : <>
          {editedValue !== defaultValue && <Icon />}
          {editedValue}
        </>
      }
    </>
  )
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!