ChatGPT解决这个技术问题 Extra ChatGPT

How do I test if a variable does not equal either of two values?

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?


M
Michał Perłakowski

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')

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)
A short version like if(x == 2|3) would be nice to have.
T
TylerH

ECMA2016 answer, especially good when checking against multiple values:

if (!["A","B", ...].includes(test)) {}

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)
Here's an old but relevant reference; tjvantoll.com/2013/03/14/…
Z
Zaz

In general it would be something like this:

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

You should probably read up on JavaScript logical operators.


Z
Z. Zlatev

I do that using jQuery

if ( 0 > $.inArray( test, [a,b] ) ) { ... }

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.
Not a fan of this at all, it seems much easier to test (test != 'A' && test != 'B') and it reads nicer
U
Unmitigated

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
}

A
AlbertVo
var test = $("#test").val();
if (test != 'A' && test != 'B'){
    do stuff;
}
else {
    do other stuff;
}

You mean test != A && test != B, else it will always execute (unless test == A == B)
@Neal: the OP wants the code executed if the value does NOT equal either one of two -> either one!
@Neal: The if() in this answer will always be true because test will always not equal one or the other.
@patrick: that's incorrect, I already put a counterexample in my first comment on this answer...
@Jurgen: that was pseudocode, read his question to see what he wants.
M
Mathlight

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;
}

R
Ryan

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;
}