2

I would like to check whether variable a is equal to b or c. Of course I know the explicit way to do this;

a === b || a === c

but is there a shorthand way of doing this in Javascript? I mean, for example,

a === (b || c)

does not work.

I found similar questions here

but they are talking about PHP.

Community
  • 1
  • 1
N. Shimode
  • 71
  • 1
  • 8

3 Answers3

7

I don't think there's a shorter or more concise way of doing this in regular JavaScript.

An alternative would be to do a ternary, but this is arguably far less readable, so I would stick with the expression you have.

Ternary:

a === b ? true : a === c

Advised, as in your question

a === b || a === c
Chris Alexander
  • 1,212
  • 14
  • 19
  • 1
    +1 Correct, there's no reasonable way in JavaScript that's more brief unless you define and reuse a function. (I think you meant "expression" rather than "expansion"...?) – T.J. Crowder Nov 30 '13 at 09:02
  • 1
    @ChrisAlexander, welcome to the site. Great first answer. – maček Nov 30 '13 at 09:05
2

The simplest way is as you described a===b||a===c all other method can only expand your code. So, Use a===b||a===c instead of using any other .

Chirag Nagpal
  • 729
  • 10
  • 25
-4
((a === (b||c))

its the shortest way

  • 4
    Short is useless if it is not equivalent. Take `a = false`, `b = false` and `c = true`. Then `a === (b || c)` is false, while `a === b || a === c` is true. – Rob W Nov 30 '13 at 09:16