0

I have a function that checks whether a user is premium by checking its flags:

isUserPremium() {
  return this.flags & Flags.PREMIUM; // returns true
}

Now let's say that I'd want another function, but this time to check whether the user is free, but using the same flag. I tried negating the returned value, but I'd like to know if there was a better way to do this.

isUserFree() {
  return !(this.flags & Flags.PREMIUM); // returns false
}
2
  • 2
    return !isUserPremium() ? Commented Jun 30, 2017 at 13:11
  • Bitwise operator & works on integers (and returns an integer), boolean operator && works on booleans (and returns a boolean). if(integer) will work in JS, but just be aware of what you are actually doing. Commented Jun 30, 2017 at 13:17

1 Answer 1

2

There isn't a way of checking whether a flag is not set with a single operator. I can suggest using !isUserPremium() instead of isUserFree() later in the code - don't create functions that invert a value returned from another function. However, make sure that you don't rely on this for security. Everything that is executing in the browser can be easily manipulated.

Sign up to request clarification or add additional context in comments.

1 Comment

I was using simplified examples to find out if I can check whether a flag is not set. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.