javascript 这段代码是否意味着“不等于”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8782494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:34:00  来源:igfitidea点击:

Does this code mean 'not equal'?

javascript

提问by BruceyBandit

I just want to know if (clickedNumber === 'Yes or No')means if the clickedNumber = 'Yes or No', then if I want to say the clickedNumber doesn't equal 'Yes or No', then is it like this:

我只想知道如果(clickedNumber === 'Yes or No')clickedNumber = 'Yes or No',那么如果我想说 clickedNumber 不等于 'Yes or No',那么是这样的:

(clickedNumber !== 'Yes or No')?

(clickedNumber !== 'Yes or No')?

Thanks

谢谢

回答by T.J. Crowder

The code you've quoted checks to see if the variable clickedNumberis strictly equalto the string "Yes or No". The opposite of that is clickedNumber !== 'Yes or No', but it's important to understand what you're dealing with.

您引用的代码检查变量clickedNumber是否严格等于string "Yes or No"。与此相反clickedNumber !== 'Yes or No',但了解您正在处理的内容很重要。

If clickedNumberis 'Yes', then clickedNumber === 'Yes or No'is false, because 'Yes'is not strictly (or loosely) equal to 'Yes or No'. Strict equality (===) means:

如果clickedNumber'Yes'clickedNumber === 'Yes or No'则为假,因为'Yes'不严格(或松散地)等于'Yes or No'。严格相等 ( ===) 表示:

  • The operands are of the same type (this is why it's "strict" rather than "loose"; "loose" equality allows casting, for instance 1 == "1").

  • Ifthe operands are of the same type, they have the same value.

  • 操作数的类型相同(这就是为什么它是“严格的”而不是“松散的”;例如,“松散”相等允许强制转换1 == "1")。

  • 如果操作数的类型相同,则它们具有相同的值。

Naturally 'Yes or No'and 'Yes'do not have the same value (they dohave the same type).

自然'Yes or No''Yes'不具有相同的值(它们确实具有相同的类型)。

If you genuinely do want to check against 'Yes or No', fair enough, but if your goal is to check against 'Yes'or 'No', individually, you must do that expressly:

如果您真的想检查'Yes or No',这很公平,但如果您的目标是检查'Yes''No',单独地,您必须明确地这样做:

if (checkedNumber === 'Yes' || checkedNumber === 'No') {
    // checkedNubmer is 'Yes' **or** it's 'No'
}

...the opposite of which is

...相反的是

if (checkedNumber !== 'Yes' && checkedNumber !== 'No') {
    // checkedNubmer is neither 'Yes' **nor** 'No'
}

回答by Merlyn Morgan-Graham

if I want to say the clickedNumber doesn't equal 'Yes or No', then is it like this:

(clickedNumber !== 'Yes or No')

如果我想说 clickedNumber 不等于“是或否”,那么它是这样的:

(clickedNumber !== 'Yes or No')

What you said is correct, but it might not give the results you expect.

你说的是对的,但它可能不会给出你期望的结果。

For example, what you currently have just checks if the string clickedNumberis equal to the string 'Yes or No':

例如,您目前只检查字符串clickedNumber是否等于字符串'Yes or No'

var clickedNumber = 'Yes or No';

if(clickedNumber === 'Yes or No')
{
    alert("check 1"); // this will be triggered
}

if(clickedNumber !== 'Yes or No')
{
    alert("check 2"); // this will *not* be triggered
}

if(clickedNumber === 'Yes')
{
    alert("check 3"); // this will *not* be triggered
}

if(clickedNumber === 'No')
{
    alert("check 4"); // this will *not* be triggered
}

It doesn't use the contents of that string to define any boolean logic, so the word ordoesn't actually do anything - it just just part of a string:

它不使用该字符串的内容来定义任何布尔逻辑,因此该词or实际上并没有做任何事情——它只是一个字符串的一部分:

var clickedNumber = 'Yes';

if(clickedNumber === 'Yes or No')
{
    alert("check 1"); // this will *not* be triggered
}

if(clickedNumber !== 'Yes or No')
{
    alert("check 2"); // this will be triggered
}

if(clickedNumber === 'Yes')
{
    alert("check 3"); // this will be triggered
}

if(clickedNumber === 'No')
{
    alert("check 4"); // this will *not* be triggered
}

If you want boolean logic to apply, then you'll have to use boolean operators in code rather than in the contents of a string:

如果要应用布尔逻辑,则必须在代码中而不是在字符串的内容中使用布尔运算符:

var clickedNumber = 'Yes';

if(clickedNumber === 'Yes' || clickedNumber === 'No')
{
    alert("check 1"); // this will be triggered
}

if(clickedNumber !== 'Yes' && clickedNumber !== 'No')
{
    alert("check 2"); // this will *not* be triggered
}

回答by nnnnnn

Yes. The code you mentioned:

是的。你提到的代码:

(clickedNumber !== 'Yes or No')

Will evaluate as trueif clickedNumberis not equal to the string 'Yes or No'. It will evalue as falseif clickedNumberis the string 'Yes or No'.

将评估为trueifclickedNumber不等于 string 'Yes or No'。这将安勤为false,如果clickedNumber是字符串'Yes or No'

Note that this is not the same as testing whether it is not equal to either of the two separate strings 'Yes'or 'No', which you would do like this:

请注意,这与测试它是否不等于两个单独的字符串'Yes'或 中的任何一个不同'No',您可以这样做:

!(clickedNumber === 'Yes' || clickedNumber === 'No')

Further reading: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Comparison_Operators

进一步阅读:https: //developer.mozilla.org/en/JavaScript/Reference/Operators/Comparison_Operators