5 Practical JavaScript Tricks to Write Cleaner Conditional Logic

This article presents five useful JavaScript techniques—using Array.includes, early returns, default parameters with destructuring, object literals or Map instead of switch, and Array.every/Array.some—to simplify and make conditional statements more readable and maintainable.

AutoHome Frontend
AutoHome Frontend
AutoHome Frontend
5 Practical JavaScript Tricks to Write Cleaner Conditional Logic

1. Use Array.includes for multiple matching criteria

Replace long || chains with Array.includes to test if a value is in a list.

function test(fruit) {
  if (['apple', 'strawberry', 'cherry', 'cranberries'].includes(fruit)) {
    console.log('red');
  }
}

2. Reduce nesting with early return

Validate inputs early and exit the function when a condition fails, flattening control flow.

function test(fruit, quantity) {
  if (!fruit) throw new Error('No fruit!');
  if (!['apple', 'strawberry', 'cherry', 'cranberries'].includes(fruit)) return;
  if (quantity > 10) console.log('big quantity');
  else console.log('red');
}

3. Use default parameters and destructuring

Assign default values directly in the function signature and destructure objects to avoid repetitive null checks.

// default parameter
function test(fruit, quantity = 1) {
  if (!fruit) return;
  console.log(`We have $${quantity} ${fruit}!`);
}

// destructuring with default empty object
function test({ name } = {}) {
  console.log(name || 'unknown');
}

4. Replace switch with object literals or Map

Use an object literal or a Map for key‑to‑value lookups instead of a switch statement.

// object literal lookup
const fruitColor = {
  red: ['apple', 'strawberry'],
  yellow: ['banana', 'pineapple'],
  purple: ['grape', 'plum']
};
function getFruitsByColor(color) {
  return fruitColor[color] || [];
}

// Map version
const fruitMap = new Map();
fruitMap.set('red', ['apple', 'strawberry']);
fruitMap.set('yellow', ['banana', 'pineapple']);
fruitMap.set('purple', ['grape', 'plum']);
function getFruitsByColorMap(color) {
  return fruitMap.get(color) || [];
}

5. Use Array.every and Array.some for collective checks

Use every to verify that all items satisfy a condition and some to verify that at least one item satisfies it.

const fruits = [
  { name: 'apple', color: 'red' },
  { name: 'banana', color: 'yellow' },
  { name: 'grape', color: 'purple' }
];
const allRed = fruits.every(f => f.color === 'red'); // false
const anyRed = fruits.some(f => f.color === 'red'); // true
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaScriptcode refactoringconditional statementsDefault Parameters
AutoHome Frontend
Written by

AutoHome Frontend

AutoHome Frontend Team

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.