简体   繁体   中英

Checking for Boolean Values in Typescript/Javascript Pertaining to “true”, “false”, and “undefined”

What's an explicit way that I can be sure that the only results for a boolean check will be "true" or "false"? In other words, I want to exclude "undefined" as a possibility as much as is possible. Two options would be:

FUNCTION ONE:

private canMove = (currentOptionSelected): boolean => {
    if (this.client.services) {
        for (const service of this.client.services) {
            if (service === currentOptionSelected) {
                if (service.currentStage === 'enrolling') {
                    return true;
                }
            }
        }
    } 
}

FUNCTION TWO:

private canMove = (currentOptionSelected): boolean => {
    if (this.client.services) {
        for (const service of this.client.services) {
            if (service === currentOptionSelected) {
                return service.currentStage === 'enrolling';
            }
        }
    }
}

EDIT: Upon a commenter's response, a more robust alternative is to explicitly return 'false', like this:

private canMove = (currentOptionSelected): boolean => {
    if (this.client.services) {
        for (const service of this.client.services) {
            if (service === currentOptionSelected) {
                //You should also rethink this return statement
                return service.currentStage === 'enrolling';
            }
        }
    }
    return false;
}

I would follow that up by asking if adding an extra "return 'false'" for a case where "client.services" exists, but "currentStage !== 'enrolling" would be even better? Or would that second 'else' clause be redundant in this case?

Secondly, he writes I should rethink the return statement in terms of where it is in the function. What would the alternative be? In short, I'm trying to find the most robust yet terse way to write this function.

These are not equivalent. The second version returns false in some of the cases where the first one returns undefined . A caller that checks the value to be explicitly === false will observe different behavior.

Both can return undefined which is probably not great. It'd be best to always return with an actual value of true or false

Short answer is No , they aren't equivalent:

  • The first one won't return false in any case.
  • While the second can return false if service.currentStage !== 'enrolling' .

But as stated by Ryan both can return undefined which you should avoid, you need to explicitly return false whenever a condition isn't met.

This is how should be your code:

private canMove = (currentOptionSelected): boolean => {
    if (this.client.services) {
        for (const service of this.client.services) {
            if (service === currentOptionSelected) {
                //You should also rethink this return statement
                return service.currentStage === 'enrolling';
            }
        }
    }
    return false;
}

Note:

  • The return false; here will make sure you return false when this.client.services is undefined .
  • Using a return statement in a for loop this way is a very very bad idea, in fact you will only make one iteration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM