67

In SQL Server, I could say:

WHERE X IN(1,2) 

How would you rewrite the following in JavaScript:

if (X==1 || X==2) {} 
2
  • I wonder if I could write an IN function.CommentedMar 9, 2011 at 21:54
  • 4
    Your question doesn't make sense. You have written valid JavaScript.
    – Oded
    CommentedMar 9, 2011 at 21:55

9 Answers 9

90

Use indexOf to see if x is in an array.

if([1,2].indexOf(x) !== -1) 
1
24

Using Array .includes method.

if ([1, 2].includes(x)) { // array has x } 
4
  • This is a better answer for modern browsers, but I'm afraid IE doesn't support it. Ref: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    – Ryan W
    CommentedJan 5, 2017 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.CommentedJan 5, 2017 at 17:16
  • Yes, IE won't work. Use it with polyfill or in conjunction with a transpiler (e.g. Babel / Typescript)
    – vitkon
    CommentedJan 5, 2017 at 22:39
  • It makes me cooler when my answer is short.CommentedJan 25, 2022 at 2: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 
2
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).

1
  • simple and super clean answer that assure to run on all browsers.CommentedJan 13, 2019 at 20:26
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

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

Requires Javascript 1.6

if ((new Array(1, 2)).indexOf(X) != -1) { } 
    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']) ) { ... } 
        1

        Just fun:

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

        or self-explaining:

        if ((X-1)*(X-2) == 0) {} 
        0

          Start asking to get answers

          Find the answer to your question by asking.

          Ask question

          Explore related questions

          See similar questions with these tags.