JavaScript: Simple way to check if variable is equal to one of two or more values? [duplicate]

You can stash your values inside an array and check whether the variable exists in the array by using [].indexOf:

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

If -1 is returned then the variable doesn’t exist in the array.

Leave a Comment