
5 tips writing code in JavaScript as a Pro
When working with JavaScript, we deal a lot with conditionals, here are the 5 tips for you to write better / cleaner conditionals.
April 18, 2026
Tip 1: Stop checking array length using [ ].length > 0.
Bad 🙈:
const mySkills = [];
if (mySkills.length > 0) {
// Write your code
} <^>Good<^> 🙉:
const mySkills = [];
if (mySkills.length) {
// Write your code
} Tip 2: Stop using single if condition, prefer to write it in one single line
Bad 🙈:
const isVisible = true; if (isVisible) {
doMyFunc()
} <^>Good<^> 🙉:
isVisible && doMyFunc() Tip 3: Stop using if to conditionally add keys in object, prefer de-structuring
Bad 🙈:
const addToCart = true;
const myObject = {};
if (addToCart) {
myObject.product = "Macbook";
} <^>Good<^> 🙉:
const addToCart = true;
const myObject = { ...(product ? { product: "Macbook" } : {}) }; Tip 4: Stop using too many conditions in if , use includes instead
Bad 🙈:
if (animal === "cat" || animal === "tiger" || animal === "dog") {
doMyFunc();
} <^>Good<^> 🙉:
const includedAnimals = ["cat", "tiger", "dog"];
if (includedAnimals.includes(animal)) {
doMyFunc()
} Tip 5: Prefer using boolean result wherever possible using '!!'
Bad 🙈:
const stringType = "DataString";
const arrayType = [];
const objectType = {};
if (stringType) {}; // Check existence of string
if (arrayType) {}; // Check existence of array
if (objectType) {}; // Check existence of object <^>Good<^> 🙉:
const stringType = "DataString";
const arrayType = [];
const objectType = {};
if (!!stringType) {}; // gives boolean result true or false
if (!!arrayType) {}; // gives boolean result true or false
if (!!objectType) {}; // gives boolean result true or false The tips above seem simple right? However, not all developers know and use it in the project to make the code shorter and easier. If you have any other tips, please let me know in the comments!