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

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

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 Report

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 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!