65

I want to write an if/else statement that tests if the value of a text input does NOT equal either one of two different values. Like this (excuse my pseudo-English code):

var test = $("#test").val();
if (test does not equal A or B){
    do stuff;
}
else {
    do other stuff;
}

How do I write the condition for the if statement on line 2?

Ryan M
  • 18,333
  • 31
  • 67
  • 74
daGUY
  • 27,055
  • 29
  • 75
  • 119

8 Answers8

150

Think of ! (negation operator) as "not", || (boolean-or operator) as "or" and && (boolean-and operator) as "and". See Operators and Operator Precedence.

Thus:

if(!(a || b)) {
  // means neither a nor b
}

However, using De Morgan's Law, it could be written as:

if(!a && !b) {
  // is not a and is not b
}

a and b above can be any expression (such as test == 'B' or whatever it needs to be).

Once again, if test == 'A' and test == 'B', are the expressions, note the expansion of the 1st form:

// if(!(a || b)) 
if(!((test == 'A') || (test == 'B')))
// or more simply, removing the inner parenthesis as
// || and && have a lower precedence than comparison and negation operators
if(!(test == 'A' || test == 'B'))
// and using DeMorgan's, we can turn this into
// this is the same as substituting into if(!a && !b)
if(!(test == 'A') && !(test == 'B'))
// and this can be simplified as !(x == y) is the same as (x != y)
if(test != 'A' && test != 'B')
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
  • 3
    Is there any shorter way to do it like this (pseudo-code): `if(test === ('A' || 'B'))` (I removed the `!` for logical simplicity, I'm more curious about the concept) – Ivan Durst Jan 18 '17 at 21:23
  • 3
    A short version like `if(x == 2|3)` would be nice to have. – Sodj Apr 16 '17 at 16:56
80

ECMA2016 answer, especially good when checking against multiple values:

if (!["A","B", ...].includes(test)) {}
TylerH
  • 20,799
  • 66
  • 75
  • 101
CESCO
  • 7,305
  • 8
  • 51
  • 83
  • 6
    This is the JavaScript way to answer the question. He wasn't asking about how to use && or || but he was looking for a shortcut that allows; test == ( 'string1' || string2) which would be equivalent to (test == 'string2') || (test == string1) – Louis Jul 19 '17 at 18:12
  • Here's an old but relevant reference; https://www.tjvantoll.com/2013/03/14/better-ways-of-comparing-a-javascript-string-to-multiple-values/ – Louis Jul 19 '17 at 18:17
  • This is niiiice, thanks. Never thought of .includes( ) this way. Kinda feels like using it "reverse", because of how I fundamentally thought about a relation between variables and their values. Equality means inclusion in both directions, wonderful :) – ikoza Apr 28 '23 at 15:34
11

In general it would be something like this:

if(test != "A" && test != "B")

You should probably read up on JavaScript logical operators.

Zaz
  • 46,476
  • 14
  • 84
  • 101
James Montagne
  • 77,516
  • 14
  • 110
  • 130
2

I do that using jQuery

if ( 0 > $.inArray( test, [a,b] ) ) { ... }
Z. Zlatev
  • 4,757
  • 1
  • 33
  • 37
  • If anyone continues to get undesired results with this then you can also check that the typeof of the test and a, b must match too if you need to get true as result. – Bhumi Singhal Apr 02 '13 at 05:33
  • 3
    Not a fan of this at all, it seems much easier to test `(test != 'A' && test != 'B')` and it reads nicer – Shannon Hochkins Feb 11 '15 at 21:51
2

For a larger number of values that is checked against often, it may be more efficient to check if the value does not exist in a Set.

const values = new Set(["a", "b"]);
if(!values.has(someValue)){
    // do something
} else {
    // do something else
}
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
0
var test = $("#test").val();
if (test != 'A' && test != 'B'){
    do stuff;
}
else {
    do other stuff;
}
AlbertVo
  • 772
  • 4
  • 9
  • 3
    You mean `test != A && test != B`, else it will always execute (unless test == A == B) – Konerak May 24 '11 at 19:33
  • @Neal: the OP wants the code executed if the value `does NOT equal either one of two` -> **either** one! – Konerak May 24 '11 at 19:35
  • @Neal: The `if()` in this answer will always be `true` because `test` will always *not* equal one or the other. – user113716 May 24 '11 at 19:35
  • @patrick: that's incorrect, I already put a counterexample in my first comment on this answer... – Konerak May 24 '11 at 19:37
  • @Jurgen: that was pseudocode, read his question to see what he wants. – Konerak May 24 '11 at 19:37
  • @Konerak: Read my comment again. I was agreeing with you. I updated the wording to make it more clear. In the original answer with `||`, the `if()` would *always* evaluate to `true` because `test` will always *not* be either `'A'` or `'B'`. – user113716 May 24 '11 at 19:41
  • ...notice that `'A'` and `'B'` are strings, so they're never equal. – user113716 May 24 '11 at 19:44
  • Heh, ok, in the answer they are literals, in the OP's question they are strings. – Konerak May 24 '11 at 19:56
0

You used the word "or" in your pseudo code, but based on your first sentence, I think you mean and. There was some confusion about this because that is not how people usually speak.

You want:

var test = $("#test").val();
if (test !== 'A' && test !== 'B'){
    do stuff;
}
else {
    do other stuff;
}
Mathlight
  • 6,436
  • 17
  • 62
  • 107
0

This can be done with a switch statement as well. The order of the conditional is reversed but this really doesn't make a difference (and it's slightly simpler anyways).

switch(test) {
    case A:
    case B:
        do other stuff;
        break;
    default:
        do stuff;
}
Ryan
  • 1
  • 1