How to write better conditionals in JavaScript


When working in any programming language, your code needs to execute actions depending on the conditions you inputted. Most notably with JavaScript, conditionals are going to be a massive part of your code. Although they may appear simple and easy to learn, conditionals are more than just if/else statements.

If you’re a developer working with JavaScript, here are useful tips for writing cleaner and better conditionals.

1. Array.includes

When you’re working on a code that includes multiple conditions, you should use Array.includes instead of listing every single item using “or” statements. When you only have two items on a list, skipping Array.includes won’t cause you issues, however, when your list grows bigger, the code looks messier, and it becomes harder to maintain. When you re-write your multiple conditions using Array.includes, it will be easier to understand and maintain. 

2. Less Nesting, Return Early

While working with a series of conditions, if/else statements, and nested if statements, the general rule to follow is to return early when there is an invalid condition found. When you do this, you will have on less level of nested statement, immediately making the code easier to manage. This coding style is useful especially when you’re working with long if statements so you won’t have to scroll to the very bottom to know there is an else statement.

You don’t always have to follow this “rule”, however. It’s still dependent on the code you are writing. You have to consider if your new version is better and more readable than your original text. What you have to keep in mind is to aim for less nesting and return early, without overdoing it.

3. Use Default Function Parameters 

You always need to check for null/defined value and assign defaults values when you’re working with JavaScript. To make your code cleaner, you can eliminate additional variables by assigning default function parameters. Each of your parameters can have its own default function parameter. When you do this, your code will be much intuitive and easier to understand. 

4. Favor Object Literal or Map instead of Switch Statement

When you have an unnecessarily lengthy code, and you want cleaner syntax, you can use either object literal or Map. Map is the object type available since ES2015. This allows you to store the key-value pair. This doesn’t mean, however, that you should stop using Switch statements altogether. You can favor object literal or map whenever possible, but you have to use whatever makes sense for your scenario.

5. Use Array.every and Array.some 

Array.every and Array.some are relatively new JavaScript Array functions, but these two can be hugely helpful for your code, as well. When utilising Array.every and Array.some, your long and messy code can be reduced into fewer lines while still performing the action required. Your code will become much cleaner, but your conditionals can be achieved in one line. 

If you’ve mastered the art of coding, you know there’s always more to learn. As a software developer, the goal is to have intuitive, simple, readable, yet powerful code that provides your end-user with what they need. April9’s software developers are highly-skilled in various types of programming languages, and they can create custom software solutions for your business. Schedule your free consultation today.