如何测试变量是否不等于两个值中的任何一个?

时间:2021-05-05 11:29:34

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

我想编写一个if / else语句来测试文本输入的值是否不等于两个不同值中的任何一个。像这样(借口我的伪英文代码):

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?

如何在第2行写if语句的条件?

7 个解决方案

#1


98  

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

考虑到 ! (否定运算符)为“not”,|| (布尔运算符或运算符)为“或”,&&(布尔运算符和运算符)为“和”。请参阅运算符和运算符优先级。

Thus:

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

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

但是,使用De Morgan定律,它可以写成:

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

上面的a和b可以是任何表达式(例如test =='B'或者它需要的任何表达式)。

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

再次,如果test =='A'和test =='B',是表达式,请注意第一种形式的扩展:

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

#2


17  

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

ECMA2016最短的答案,特别适合检查多个值:

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

#3


8  

In general it would be something like this:

一般来说,它会是这样的:

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

You should probably read up on JavaScript logical operators.

您应该阅读JavaScript逻辑运算符。

#4


2  

I do that using jQuery

我使用jQuery做到这一点

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

#5


1  

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

#6


1  

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

#7


0  

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 / else语句中使用else if语句。如果您不想运行任何不在任何条件下的代码,您可以在语句结尾处留下其他内容。否则,如果还可以用于任何数量的转移路径,需要每个转换路径都是特定条件。

if(condition 1){

} else if (condition 2) {

} else if(condition 2){

}else {

}

#1


98  

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

考虑到 ! (否定运算符)为“not”,|| (布尔运算符或运算符)为“或”,&&(布尔运算符和运算符)为“和”。请参阅运算符和运算符优先级。

Thus:

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

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

但是,使用De Morgan定律,它可以写成:

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

上面的a和b可以是任何表达式(例如test =='B'或者它需要的任何表达式)。

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

再次,如果test =='A'和test =='B',是表达式,请注意第一种形式的扩展:

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

#2


17  

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

ECMA2016最短的答案,特别适合检查多个值:

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

#3


8  

In general it would be something like this:

一般来说,它会是这样的:

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

You should probably read up on JavaScript logical operators.

您应该阅读JavaScript逻辑运算符。

#4


2  

I do that using jQuery

我使用jQuery做到这一点

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

#5


1  

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

#6


1  

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

#7


0  

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 / else语句中使用else if语句。如果您不想运行任何不在任何条件下的代码,您可以在语句结尾处留下其他内容。否则,如果还可以用于任何数量的转移路径,需要每个转换路径都是特定条件。

if(condition 1){

} else if (condition 2) {

} else if(condition 2){

}else {

}