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

181
Views
Cleaner way and alternative to write if and else with JS checks

What are some alternatives to write cleaner, better, and smarter if/else function?

I am actually trying to convert my code to something more readable.

 const statusComment = () => {
      const isApproved = "๐ŸŸข";
      const unApproved = "๐Ÿ”ด";

      let status;

      // is approved and has comment.
      if (checkApproval && comment) {
        status = `${isApproved}  โ€” ${comment}`;
        // is not approved and has comment.
      } else if (!checkApproval && comment) {
        status = `${unApproved}  โ€” ${comment}`;
        // has comment.
      } else if (comment) {
        status = comment;
        // is approved.
      } else if (checkApproval) {
        status = isApproved;
      } else {
        // is not approved.
        status = unApproved;
      }

      return status;
    };
about 4 years ago ยท Juan Pablo Isaza
2 answers
Answer question

0

Let's break it down a bit: your string has two parts that are joined by a dash -:

  1. The first part is always present: it depends on the checkApproval boolean value.
  2. The second part is optional and depends on the truthy status of comment.

Also, do note that your third statement is not possible to get to, since comment is always truthy in the first two statements and it's combined with a boolean flag.

Therefore, you can create an array with the compulsory first part with this ternary statement:

const status = [checkApproval ? "๐ŸŸข" : "๐Ÿ”ด"];

For the second optional part, you push into it when comment is truthy:

if (comment) status.push(comment);

Here's your modified code:

const statusComment = () => {
  const status = [checkApproval ? "๐ŸŸข" : "๐Ÿ”ด"];
  if (comment) status.push(comment);

  return status.join(' โ€” ');
};
about 4 years ago ยท Juan Pablo Isaza Report

0

Let's make a table based on your existing code:

checkApproval     comment        out
true              true           green - comment
false             true           red - comment
                  true           comment         duplicate/unnecessary?
true                             green
false                            red

So we see that if there is true for checkApproval, it's green, and red otherwise.

Let's make that right now:

const status = `${checkApproval ? "๐ŸŸข" : "๐Ÿ”ด"}`;

And if there's a comment, there is a dash followed by the comment. This we can represent too:

const status = `${checkApproval ? "๐ŸŸข" : "๐Ÿ”ด"}${comment ? " - " + comment : ""}`;

If there is no comment, nothing is added after.

Then we can just return status normally.

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!