Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 504429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:29:46+00:00 2026-05-13T06:29:46+00:00

I’m trying to write more readable and more efficient code, I’ve always hated if

  • 0

I’m trying to write more readable and more efficient code, I’ve always hated if if if chains .. is it possible to write the following statement in some other more “elegant” way like maybe using ternary ops :

if(text.length < 1){
    if(errtext.length > 1){
        errtext+="<br>";
    }
    errors = true;
    errtext += "Field cannot be empty";
}

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:29:46+00:00Added an answer on May 13, 2026 at 6:29 am

    Honestly, in most cases you want to use the if chains because they’re more clear to a reader. There are a few exceptions, but not many.

    Often if your code is awkward, your fundamental approach is wrong. Try this:

    function validate()
    {
      errs = [];
      // ...
      if (!txt.length) {
        errs.push("Must have text, man!");
      }
      // ...
      return errs.join("<BR>");
    }
    

    It bears mentioning that strings are immutable in Javascript. If you += on strings, you’re creating a lot of intermediary strings, which can slow down performance.

    E.G.

    var msg = "Tom";
    msg += ", Brad";
    msg += ", Sam";
    msg += ", Rick";
    

    This ends up creating the following strings in memory:

    "Tom"
    "Brad"
    "Sam"
    "Rick"
    "Tom, Brad"
    "Tom, Brad, Sam"
    "Tom, Brad, Sam, Rick"
    

    Where as the following:

    var msg = ["Tom", "Brad", "Sam", "Rick"];
    var msg = msg.join(", ");
    

    creates the following in memory:

    "Tom"
    "Brad"
    "Sam"
    "Rick"
    "Tom, Brad, Sam, Rick"
    

    Wouldn’t make a big difference in this case, most likely, but it’s good to get into the habit of creating arrays of strings and using the join method. In larger text manipulation/generation problems, it can cause a huge slowdown.

    EDIT:

    If the calling function needs to check for the presence or absence of errors, you can always do an if on the .length of the returned string. If you want to be more explicit, you can always do this:

    if (!errs.length) {
      return null; // or false, or a constant...
    }
    return errs.join("<BR>");
    

    and then add a check in your calling code (=== null, etc.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 441k
  • Answers 441k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Well, I was bored last night so I did this… May 15, 2026 at 5:35 pm
  • Editorial Team
    Editorial Team added an answer Use the readline module. May 15, 2026 at 5:35 pm
  • Editorial Team
    Editorial Team added an answer Basically you will want to store each item the user… May 15, 2026 at 5:35 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.