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

364
Views
Javascript: using ternary operator to return default value if a function returns undefined

given this function

function isTastyCheese(cheeseName: string) {
    const TastyCheeses = ['camembert', 'maroilles']
    if (TastyCheeses.includes(cheeseName)) {
        return 'yes so tasty'
    }
    return undefined
}

I'd like to do something like this:

const answerCamembert = isTastyCheese('camembert') ? the_returned_value : 'no taste like sh*t' 
const answerCantal = isTastyCheese('cantal') ? the_returned_value : 'no taste like sh*t'

console.log(answerCamembert)
>>> 'yes so tasty'
console.log(answerCantal)
>>> 'no taste like sh*t'

How do i use ternary operator to do this concisely?

PS: Obsviously the function presented here is very wrong but changing it shouldn't be part of the solution

about 4 years ago · Juan Pablo Isaza
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!