Search code examples
javascriptlogiccontrol-flow

Using multiple logical "or" operators


Other than using a switch statement (or writing if(x === 2 || x === 3 || x === 4) etc), is there any way to implement multiple "or" (||) operators?

E.g.:

if(x === 2 || 3)
    alert("Yes");

This alerts for every value of x


Solution

  • The closest you can probably come is to do something like this:

    if ([2,3].indexOf(x) > -1){
    }
    

    DOCS

    Of course that will require a shim for IE 8 and below, if that's an issue for you.