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

245
Views
javascript if..else statement to ? :

newbie here. Thanks for your helps.

I'm wondering can I change if ...else ... to ? : statement in following codes:

https://codepen.io/lgtits/pen/MWoqPBX?editors=1010

list.addEventListener('click', function (event) {
  let target = event.target
  if (target.classList.contains('delete')) {
    let parentElement = target.parentElement
    parentElement.remove()
  } else if (target.tagName === 'LABEL') { 
    target.classList.toggle('checked')
  }
})

just like this:

let a = 1

let b = 2

if(a>b) {
  console.log('bigger')
} else if (a === b) {
  console.log('equal')
} else {
  console.log('smaller')
}

a > b ? console.log('bigger') : 
      a === b ? console.log('equal') :
      console.log('smaller')
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could change the first block if...else if to the following:

list.addEventListener("click", function (event) {
  let target = event.target;
  // set this ahead of time if needed
  let parentElement = target.parentElement;
  target.classList.contains("delete")
    ? parentElement.remove()
    : target.tagName === "LABEL"
      ? target.classList.toggle("checked")
      : void 0;
});

It can tend to get clunky if you have too many different things to check for, but it's doable if you're only doing one operation each in if and else blocks. Just remember to make sure it is readable.

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!