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
  • 1,699
  • 8
  • 26

2 Answers2

2

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
  • 5,593
  • 3
  • 20
  • 44
0

Try this:

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