It’s fun to try coding without if, and what if you get the idea that “data is code, code is data”?

To ensure readability, free translation rather than literal translation is used in this paper. In addition, the copyright of this article belongs to the original author, and translation is for study only.

When I teach beginners programming, I like to give them little challenges, such as solving programming problems without using if statements (or ternary operators, switch statements, etc.).

What’s the point of doing that?

In fact, it can force you to look at solutions from a different Angle and maybe find a better way.

Of course, there is nothing wrong with using the if statement. However, not using if can sometimes make your code more readable. This isn’t always true, and the code might be less readable if you didn’t use the if statement at all. It’s up to you to judge on a case-by-case basis.

And not using an IF doesn’t just affect readability. Behind this lies a more profound truth. By looking at the code examples in this article, you can see that your code is closer to the concept of code as data if you don’t use the if statement.

It’s also interesting when you try to program without an if statement.

### Example 1: Odd numbers in a statistical array

Suppose we have an arrayOfIntegers and now need to count the number of odd numbers:

``````const arrayOfIntegers = [1, 4, 5, 9, 0, -1, 5];
Copy the code``````

#### Use the if

``let counter = 0; arrayOfIntegers.forEach((integer) => { const remainder = Math.abs(integer % 2); if (remainder === 1) { counter++; }}); console.log(counter);Copy the code``

#### Don’t have the if

``````let counter = 0;

arrayOfIntegers.forEach((integer) => {
const remainder = Math.abs(integer % 2);
counter += remainder;
});

console.log(counter);
Copy the code``````

When we don’t use if, we take advantage of odd and even numbers, which divide by 2 and have a remainder of 0 and 1, respectively.

### Example 2: Determine weekdays and weekends

Given a Date(such as new Date()), determine whether it is a weekday or a weekend, returning “weekend” and “weekday”, respectively.

#### Use the if

``````const weekendOrWeekday = (inputDate) => {
const day = inputDate.getDay();

if (day === 0 || day === 6) {
return 'weekend';
}

return 'weekday';

// Or, for ternary fans:
// return (day === 0 || day === 6) ? 'weekend' : 'weekday';
};

console.log(weekendOrWeekday(new Date()));
Copy the code``````

#### Don’t have the if

``````const weekendOrWeekday = (inputDate) => {
const day = inputDate.getDay();

return weekendOrWeekday.labels[day] ||
weekendOrWeekday.labels['default'];
};

weekendOrWeekday.labels = {
0: 'weekend',
6: 'weekend',
default: 'weekday'
};

console.log(weekendOrWeekday(new Date()));
Copy the code``````

Do you see any hidden information in the if statement? It tells us which day is a weekend and which day is a weekday. Therefore, to remove the if statement, we simply write this information to the weekendorweekday. labels object and use it directly.

### Example 3: The Doubler function

Write a Doubler function that does different things depending on the type of argument:

• If the parameter is numeric, multiply it by 2(I.E.`5 = > 10`.`- = > 10-20`);
• If the argument is a string, each character is repeated twice (I.E.`'hello' => 'hheelloo'`);
• If the argument is a function, it is called twice;
• If the arguments are arrays, the Doubler function is called with each element as an argument
• If the argument is an object, the Doubler function is called with each attribute value as an argument

#### Use the switch

``const doubler = (input) => { switch (typeof input) { case 'number': return input + input; case 'string': return input .split('') .map((letter) => letter + letter) .join(''); case 'object': Object.keys(input) .map((key) => (input[key] = doubler(input[key]))); return input; case 'function': input(); input(); }}; console.log(doubler(-10)); console.log(doubler('hey')); console.log(doubler([5, 'hello'])); console.log(doubler({ a: 5, b: 'hello' })); console.log( doubler(function() { console.log('call-me'); }));Copy the code``

#### Don’t have to switch

``const doubler = (input) => { return doubler.operationsByType[typeof input](input); }; doubler.operationsByType = { number: (input) => input + input, string: (input) => input .split('') .map((letter) => letter + letter) .join(''), function: (input) => { input(); input(); }, object: (input) => { Object.keys(input) .map((key) => (input[key] = doubler(input[key]))); return input; }}; console.log(doubler(-10)); console.log(doubler('hey')); console.log(doubler([5, 'hello'])); console.log(doubler({ a: 5, b: 'hello' })); console.log( doubler(function() { console.log('call-me'); }));Copy the code``

As you can see, I bind the operations corresponding to each parameter type to Doubler.OperationsByType so that the Doubler function can be implemented without the need for a switch statement.