DEV Community


Discussion on: 5 ways to refactor if/else statements in JS functions

eecolor profile image

If you use async your code becomes a bit more natural:

async function divide(numerator, denominator) {
    if (typeof numerator !== "number") {
        throw new TypeError(`numerator should be a number in divide(${numerator}, ${denominator})`);

    if (typeof denominator !== "number") {
        throw new TypeError(`denominator should be a number in divide(${numerator}, ${denominator})`);

    if (0 === denominator) {
        throw new Error(`denominator should not be zero in divide(${numerator}, ${denominator})`);

    return numerator / denominator;
Enter fullscreen mode Exit fullscreen mode

This however indicates that it would be probably be smart to remove the async and move that to a separate function. That would allow you to use the divide function in more scenario's.