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

182
Views
How to use ternary operator to render jsx using react and javscript?

i have code like below,

{hideValue && !isOpen && (
    <span>
      {percentage}
    </span>
  )}
  {!hideValue && !isOpen && (
    <span>
      {value}
    </span>
  )}

how to rewrite above code using ternary operator?

I am new to ternary operator usage. could someone help me with this. thanks.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think what you have it's ok (maybe there's some duplicated code), but you can indeed use a ternary operator.

A ternary operator is basically a shorthand way of writing an if-else statement. In this case, if hideValue is true we want to show percentage otherwise we show value.

Something like this:

{!isOpen && 
  (hideValue ? <span>{percentage}</span> : <span>{value}</span>
)}

Note that since !isOpen always needs to be false you can very it before.

You can additionally implement it like this:

{!isOpen && <span> {hideValue ? percentage : value} </span>}

since they both use the <span> tag.

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!