68

In SQL Server, I could say:

WHERE X IN(1,2)

How would you rewrite the following in JavaScript:

if (X==1 || X==2) {}
Piyush Mattoo
  • 15,454
  • 6
  • 47
  • 56
Phillip Senn
  • 46,771
  • 90
  • 257
  • 373

9 Answers9

90

Use indexOf to see if x is in an array.

if([1,2].indexOf(x) !== -1)
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Peter Olson
  • 139,199
  • 49
  • 202
  • 242
  • Browser compatibility of Array.indexOf looks good, starts with MSIE 9 for example https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf#Browser_compatibility – Open SEO Apr 21 '20 at 16:16
24

Using Array .includes method.

if ([1, 2].includes(x)) {
  // array has x
}
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
vitkon
  • 1,058
  • 8
  • 13
  • This is a better answer for modern browsers, but I'm afraid IE doesn't support it. Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes – Ryan Wu Jan 05 '17 at 16:19
  • 6
    It's good practice on Stack Overflow to add an explanation as to why your solution should work. For more information read [How To Answer](http://stackoverflow.com/help/how-to-answer). – Fabian Schultz Jan 05 '17 at 17:16
  • Yes, IE won't work. Use it with polyfill or in conjunction with a transpiler (e.g. Babel / Typescript) – vitkon Jan 05 '17 at 22:39
  • It makes me cooler when my answer is short. – NeoZoom.lua Jan 25 '22 at 02:18
9

Try using an array, and then its .indexOf().

 var myNumbers = [1,2];
 var foo = 4;
 var bar = 1;

 var exists = (myNumbers.indexOf(bar) > -1); //true
 var notExists = (myNumbers.indexOf(foo) > -1); //false
p.campbell
  • 98,673
  • 67
  • 256
  • 322
  • Note: Not all browsers come with `Array.indexOf` – adamJLev Mar 09 '11 at 22:01
  • 3
    IE7 and below don't support it apparently.. here's more info and a drop in fallback http://soledadpenades.com/2007/05/17/arrayindexof-in-internet-explorer/ – adamJLev Mar 09 '11 at 22:14
7

There's no silver bullet. There will be a few gotchas.

If you do indexOf as some answers suggest, you need to remember that Array.indexOf is not supported in all browsers, so you need to provide your own fallback. Also this will have a performance of O(n) since it needs to traverse the whole array, which might not be ideal if you're dealing with a huge array.

If you use the in operator as other answers suggest, you need to remember that in Javascript object's properties are always strings, so don't expect === checks to work if you're checking for numbers.

In this particular example you suggested, I would just go for the good old if (X==1 || X==2).

adamJLev
  • 13,713
  • 11
  • 60
  • 65
3
if (x in {1:true, 2:true}) { }

Or, if you want to abstract it you could do it like this http://snook.ca/archives/javascript/testing_for_a_v

function oc(a) { var o = {}; for(var i=0;i

Still... not the most scalable thing to be doing

Tom Gruner
  • 9,635
  • 1
  • 20
  • 26
  • 2
    This fails If `x` contains an identifier of an “inherited” property: `var x = "constructor"; x in {1:true, 2:true}` yields true. – Gumbo Mar 09 '11 at 21:58
  • Thats interesting, to be honest it isn't really the nicest construct anyway. – Tom Gruner Mar 09 '11 at 22:03
2

Requires Javascript 1.6

if ((new Array(1, 2)).indexOf(X) != -1) {
}
vbence
  • 20,084
  • 9
  • 69
  • 118
2

I know we have in_array() function in PHP, but I never heard of a similar function in JS. I think you gotta do it the old way:

function contains(a, obj) {
  var i = a.length;
  while (i--) {
    if (a[i] === obj) {
      return true;
    }
  }
  return false;
}
2

Function to convert the array into an object literal

function oc(a)
{
 var o = {};
 for(var i=0;i<a.length;i++)
 {
  o[a[i]]='';
 }
  return o;
}

You can call the function like

if( name in oc(['Tom', 'Harry','Sue']) ) { ... }
Piyush Mattoo
  • 15,454
  • 6
  • 47
  • 56
1

Just fun:

if (X*X+1*2 == (1+2)*X) {}

or self-explaining:

if ((X-1)*(X-2) == 0) {}
ypercubeᵀᴹ
  • 113,259
  • 19
  • 174
  • 235