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

JavascriptIf StatementConditional StatementsEqualsBoolean Logic

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

ECMA2016 Shortest answer, specially good when checking againt multiple values:

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

Solution 3 - Javascript

In general it would be something like this:

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

You should probably read up on JavaScript logical operators.

Solution 4 - Javascript

I do that using jQuery

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

Solution 5 - Javascript

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
}

Solution 6 - Javascript

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

Solution 7 - Javascript

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

Solution 8 - Javascript

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

Solution 9 - Javascript

if([a,b].includes(test)) {
    do stuff
}

Solution 10 - Javascript

May I suggest trying to use in else if statement in your if/else statement. And if you don't want to run any code that not under any conditions you want you can just leave the else out at the end of the statement. else if can also be used for any number of diversion paths that need things to be a certain condition for each.

if(condition 1){

} else if (condition 2) {

}else {

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondaGUYView Question on Stackoverflow
Solution 1 - Javascriptuser166390View Answer on Stackoverflow
Solution 2 - JavascriptCESCOView Answer on Stackoverflow
Solution 3 - JavascriptJames MontagneView Answer on Stackoverflow
Solution 4 - JavascriptZ. ZlatevView Answer on Stackoverflow
Solution 5 - JavascriptUnmitigatedView Answer on Stackoverflow
Solution 6 - JavascriptAlbertVoView Answer on Stackoverflow
Solution 7 - JavascriptsophistihipView Answer on Stackoverflow
Solution 8 - JavascriptRyanView Answer on Stackoverflow
Solution 9 - JavascriptMrCoderView Answer on Stackoverflow
Solution 10 - JavascriptJohnnyBoyyView Answer on Stackoverflow