javascript - Does this code mean 'not equal'? - Stack Overflow

I just want to know if (clickedNumber === 'Yes or No') means if the clickedNumber = 'Yes

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

Thanks

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

Thanks

Share Improve this question edited Jan 8, 2012 at 23:51 Ufuk Hacıoğulları 38.5k14 gold badges118 silver badges157 bronze badges asked Jan 8, 2012 at 23:49 BruceyBanditBruceyBandit 4,33421 gold badges80 silver badges167 bronze badges 5
  • That does NOT check if both clickedNumber !== 'Yes' || clickedNumber !== 'No'. It just checks that the variable clickedNumber does not literally equal Yes or No. – Jared Farrish Commented Jan 8, 2012 at 23:52
  • Are you checking that the string is equal to the 'Yes or No' string, or if it's equal to either 'Yes' or 'No'? – David Thomas Commented Jan 8, 2012 at 23:53
  • Well the problem is I cannot see the difference because of a mishap in my code. (Long story). So I can't test it so I want to know if this is how it is done – BruceyBandit Commented Jan 8, 2012 at 23:53
  • Yes and No is in one string, I am not checking for both – BruceyBandit Commented Jan 8, 2012 at 23:54
  • 1 If you're checking that a string value is equal to Yes or No, then yes, you are right (in this case, not sure that != wouldn't be the same, since type checking is a bit of a non-issue in this case). – Jared Farrish Commented Jan 8, 2012 at 23:57
Add a ment  | 

3 Answers 3

Reset to default 10

The code you've quoted checks to see if the variable clickedNumber is strictly equal to 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.

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

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

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

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

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:

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

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

(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 clickedNumber is equal to the string '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 or doesn't actually do anything - it just just part of a string:

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
}

Yes. The code you mentioned:

(clickedNumber !== 'Yes or No')

Will evaluate as true if clickedNumber is not equal to the string 'Yes or No'. It will evalue as false if clickedNumber is the string '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:

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743621047a4479780.html

相关推荐

  • javascript - Does this code mean 'not equal'? - Stack Overflow

    I just want to know if (clickedNumber === 'Yes or No') means if the clickedNumber = 'Yes

    21小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信