0

To check if x variable is equal to 1 or 2, i would do normally something like:

if (x === 1 || x === 2){
   // ....
}

but this can get in some cases very cumbersome.

Edit :I'm working right now on this and writing the fonction name every time i think can be done in a cleaner manner:

if (
      this.getNotificationStatus() === 'denied' ||
      this.getNotificationStatus() === 'blocked'
    )

Is there any other lighter way to write this?

THANKS

B. Mohammad
  • 2,152
  • 1
  • 13
  • 28

2 Answers2

3

You could do:

if ([1, 2].includes(x)) {
  // ....
}

Or:

if ([1, 2].indexOf(x) > -1) {
  // ....
}

Or:

switch (x) {
  case 1:
  case 2:
    // ....
    break;
  default:
}

I don't think they're "lighter" than your solution though.

technophyle
  • 7,972
  • 6
  • 29
  • 50
0

Try this:

if ([1, 2, 3].includes(x)) {
  // your code here
}
Grey Chanel
  • 212
  • 1
  • 5