10 Clever Ways to Replace if‑else in JavaScript

This article presents ten practical techniques—including object mapping, Array.includes, ternary chains, logical operators, switch patterns, Proxy, functional programming, state machines, and decorators—to simplify and shorten JavaScript conditional logic, each illustrated with clear code examples.

JavaScript
JavaScript
JavaScript
10 Clever Ways to Replace if‑else in JavaScript

If you use traditional if‑else statements for complex conditions, the code can quickly become verbose and hard to maintain. Below are several alternative approaches.

1. Object mapping replaces if‑else

Traditional code

function getPrice(user) {
    if (user.type === 'vip') {
        return 'VIP价格';
    } else if (user.type === 'svip') {
        return 'SVIP价格';
    } else if (user.type === 'vvip') {
        return 'VVIP价格';
    } else {
        return '普通价格';
    }
}

Alternative code

const priceStrategy = {
    vip: () => 'VIP价格',
    svip: () => 'SVIP价格',
    vvip: () => 'VVIP价格',
    default: () => '普通价格'
};

function getPrice(user) {
    return (priceStrategy[user.type] || priceStrategy.default)();
}

2. Array.includes replaces multiple conditions

Traditional code

if (status === 'failed' || status === 'error' || status === 'rejected') {
    handleError();
}

Alternative code

const errorStatus = ['failed', 'error', 'rejected'];
if (errorStatus.includes(status)) {
    handleError();
}

3. Ternary operator chain

Traditional

Alternative

4. Clever use of && and || operators

5. Switch pattern matching

6. Using Proxy for condition interception

7. Functional programming methods

8. State machine pattern

9. Using decorators to handle conditions

function checkPermission(target, name, descriptor) {
    const original = descriptor.value;
    descriptor.value = function(...args) {
        if (this.user?.hasPermission) {
            return original.apply(this, args);
        }
        throw new Error('No permission');
    };
    return descriptor;
}

class Document {
    @checkPermission
    edit() {
        // 编辑文档
    }
}

Feel free to add more.

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.

Design PatternsJavaScriptCode Refactoringprogramming techniquesConditional Logic
JavaScript
Written by

JavaScript

Provides JavaScript enthusiasts with tutorials and experience sharing on web front‑end technologies, including JavaScript, Node.js, Deno, Vue.js, React, Angular, HTML5, CSS3, and more.

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.